C++ Articles

Page 13 of 597

4 Dimensional Array in C/C++

Farhan Muhamed
Farhan Muhamed
Updated on 15-Mar-2026 1K+ Views

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 More

An Uncommon representation of array elements in C/C++

Nitya Raut
Nitya Raut
Updated on 15-Mar-2026 165 Views

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 More

Initialization of a multidimensional arrays in C/C++

Jennifer Nicholas
Jennifer Nicholas
Updated on 15-Mar-2026 304 Views

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 More

How to detect integer overflow in C/C++?

Vrundesha Joshi
Vrundesha Joshi
Updated on 15-Mar-2026 464 Views

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 More

C/C++ Macro for string concatenation

Farhan Muhamed
Farhan Muhamed
Updated on 15-Mar-2026 5K+ Views

In C programming, macros provide a powerful way to concatenate strings at compile-time using the preprocessor. This technique allows for efficient string manipulation without runtime overhead, making it particularly useful for creating constants, debug messages, and code generation. Syntax /* String literal concatenation */ #define STRING1 "First part" #define STRING2 "Second part" #define CONCATENATED STRING1 STRING2 /* Token concatenation using ## operator */ #define CONCAT_TOKENS(a, b) a##b Understanding Macros in C In C, macros are preprocessor directives defined using #define. They are expanded before compilation, replacing every occurrence with the defined value. Macros ...

Read More

Does Ternary operation exist in MySQL just like C or C++?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 173 Views

Yes, ternary operation exists in MySQL using the CASE WHEN statement, which provides similar conditional logic to the ternary operator in C/C++. Let us first examine how the ternary operator works in C and then see its MySQL equivalent. Syntax C Ternary Operator: condition ? value_if_true : value_if_false MySQL Equivalent: CASE WHEN condition THEN value_if_true ELSE value_if_false END Example: C Ternary Operator Here is a complete C program demonstrating the ternary operator − #include int main() { int X = 5; ...

Read More

Why array index starts from zero in C/C++ ?

Ravi Ranjan
Ravi Ranjan
Updated on 15-Mar-2026 7K+ Views

An array arr[i] is interpreted as *(arr+i). Here, arr denotes the address of the first array element or the 0th index element. So *(arr+i) means the element is at i distance from the first element of the array. Therefore, array index starts from 0 as initially i is 0 which means the first element of the array. In this article, we will see examples of C code to understand the reason why an array starts from index 0. Syntax data_type array_name[array_size]; // Accessing element: array_name[index] where index starts from 0 Why Array Index Starts ...

Read More

Why do we assume strncpy insecure in C/C++?

Ravi Ranjan
Ravi Ranjan
Updated on 15-Mar-2026 699 Views

The strncpy() function is used to copy a specified number of characters from a source string to a destination string. While it might seem safer than strcpy() because it limits the number of characters copied, strncpy() has several security vulnerabilities that make it unsafe for general use. Syntax char *strncpy(char *destination, const char *source, size_t n); Parameters: destination: Pointer to the destination array where content is to be copied source: Pointer to the source string to be copied n: Maximum number of characters to be copied from source Why strncpy() is Insecure ...

Read More

Accessing array out of bounds in C/C++

Ravi Ranjan
Ravi Ranjan
Updated on 15-Mar-2026 2K+ Views

An array in C is a fixed-size sequential collection of elements of the same data type where all elements are stored in contiguous memory. When an array is accessed out of bounds, undefined behavior occurs in C, unlike higher-level languages that throw exceptions. Syntax type arrayName[size]; // Valid indices: 0 to (size-1) // Out of bounds: index < 0 or index >= size Accessing Out of Bounds Memory Accessing out-of-bounds memory means trying to access an array index outside its valid range (index < 0 or index >= array size). This returns garbage values ...

Read More

How to write long strings in Multi-lines C/C++?

Ravi Ranjan
Ravi Ranjan
Updated on 15-Mar-2026 15K+ Views

To write long strings in multi-lines, you can use the 'newline character' or 'string concatenation'. It increases the readability of the code, makes the code look clean, and you can avoid horizontal scrolling. In this article, we have a long string and our task is to write the long string in multi-lines in C. Syntax // Using newline character char str[] = "First lineSecond lineThird line"; // Using string concatenation char str[] = "First part of string " "Second part of string ...

Read More
Showing 121–130 of 5,962 articles
« Prev 1 11 12 13 14 15 597 Next »
Advertisements