Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 1010 of 2547
Levels of Pointers in C/C++
In C, pointers have multiple levels, which means a pointer can point to another pointer − so the chains of indirection can go on and on. For instance, a pointer to a variable's address is stored at "*ptr" (single-level pointer) while, at "**ptr", the address of another pointer is kept (a double-level pointer), and so on. This is useful in allocating memory dynamically, working with multi-dimensional arrays, and handling complicated data structures. Syntax // Single level pointer int *ptr = &variable; // Double level pointer int **pptr = &ptr; // Triple level ...
Read MoreStandard Size of character (\'a\') in C/C++ on Linux
In C, every character including 'a' is stored using a specific size in memory. On most systems including Linux, the size of a character is 1 byte. This means that any character (like 'a') occupies 1 byte (8 bits) of memory. To determine how much memory is used by the character 'a', we can use the sizeof() operator, which returns the size in bytes of a variable or data type. Syntax sizeof(expression) sizeof(datatype) Method 1: Using sizeof with Character Literal This approach checks the size of the character literal 'a' using the sizeof() ...
Read MoreFind out the current working directory in C/C++
To find the Current Working Directory (CWD) in C or C++ is like asking your program: "Hey, where am I right now?". Simply we can say that it is like a folder of your program which is present and used to operate in. We can use functions like getcwd() from unistd.h in C/C++ or filesystem::current_path() from C++17. Below are the list of the ways to achieve this − Using getcwd() in C/C++ Using filesystem in C++17 Using getcwd() Function in C In C, we use the ...
Read MoreSum of array using pointer arithmetic in C
In C programming, we can calculate the sum of array elements using pointer arithmetic. This approach uses pointers to traverse the array and access elements without using array indexing notation. Pointer arithmetic allows us to navigate through memory addresses. When we use *(a + i), it accesses the element at position i from the base address a. This is equivalent to a[i] but demonstrates direct memory manipulation. Syntax int sum = sum + *(pointer + index); Example Here's a complete program that calculates the sum of array elements using pointer arithmetic − ...
Read More4 Dimensional Array in C/C++
A 4 dimensional array in C is an array of 3 dimensional arrays, where each 3D array contains multiple 2D arrays, and each 2D array contains multiple 1D arrays. This creates a hierarchical structure with four levels of indexing. Syntax datatype array_name[dimension1][dimension2][dimension3][dimension4]; For example: int arr[3][2][3][4]; /* 3 blocks, 2 tables, 3 rows, 4 columns */ Declaration and Initialization You can initialize a 4D array during declaration using nested braces − #include int main() { int arr[2][2][2][3] = { ...
Read MoreAn Uncommon representation of array elements in C/C++
In C, array elements can be accessed in several ways. While the most common method is using the subscript operator (array[i]), we can also use pointer arithmetic to access elements in an uncommon but valid representation. Syntax *(array + index) // Equivalent to array[index] Example: Pointer Arithmetic Array Access This example demonstrates how to access array elements using pointer arithmetic instead of the traditional subscript notation − #include int main() { int array[5] = {7, 7, 7, 6, 6}; ...
Read MoreWhy C treats array parameters as pointers?
In C, array parameters are automatically converted to pointers when passed to functions. This behavior is a fundamental design decision that improves memory efficiency and prevents unnecessary copying of large data structures. Syntax void function(int arr[]); // Array parameter syntax void function(int *arr); // Equivalent pointer syntax void function(int arr[10]); // Size is ignored Why Arrays Become Pointers When you pass an array to a function, C automatically passes the address of the first element instead of copying the entire array. This approach offers several advantages ...
Read MoreInitialization of a multidimensional arrays in C/C++
Multidimensional arrays in C are arrays of arrays, where each element is itself an array. They are used to represent data in multiple dimensions like matrices, tables, or grids. The most common type is a 2D array, but C supports arrays of any dimension. 3D Array Memory Layout (2×2×2) Layer 0 [0][0][0] [0][0][1] [0][1][0] ...
Read MoreHow to detect integer overflow in C/C++?
Integer overflow occurs when an arithmetic operation produces a result that exceeds the maximum value that can be stored in the data type. Detecting overflow before it happens is crucial for preventing undefined behavior and security vulnerabilities in C programs. Syntax // For addition overflow detection if (a > MAX_VALUE - b) { // Overflow would occur } // For unsigned addition if (result < operand1 || result < operand2) { // Overflow occurred } Method 1: Pre-computation Check for Signed Integers The safest approach ...
Read MoreConvert a floating point number to string in C
In C, converting a floating point number to a string is accomplished using the sprintf() function. This function works similarly to printf(), but instead of printing to the console, it writes the formatted output to a string buffer. Syntax int sprintf(char *str, const char *format, ...); Parameters: str − Pointer to the destination string buffer format − Format string specifier (e.g., "%f" for float) ... − Variable arguments to be formatted Example 1: Basic Float to String Conversion The following example demonstrates converting a floating point number to a string using ...
Read More