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
C Articles
Page 88 of 96
Sum 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 MoreC/C++ Macro for string concatenation
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 MoreC program to print a string without any quote in the program
This is another tricky problem. In this program, we will see how to print a string using C where no quotation marks are used in the source code. Here we are using a macro function with the stringizing operator. We define a macro function that converts its argument into a string literal at compile time. Syntax #define getString(x) #x The getString() is a macro function that returns x by converting it into a string. The # before x is the stringizing operator that converts the macro argument into a string literal. Example ...
Read MoreC Program to display hostname and IP address
In C, we can retrieve the hostname and IP address of the local system using socket programming functions. This is useful for network programming and system administration tasks. Syntax int gethostname(char *name, size_t len); struct hostent *gethostbyname(const char *name); char *inet_ntoa(struct in_addr in); Key Functions Function Description gethostname() Retrieves the standard hostname for the local computer gethostbyname() Finds host information corresponding to a hostname from host database inet_ntoa() Converts an IPv4 network address into ASCII dotted decimal format ...
Read More