C Language: What is the difference between char array and char pointer in C language with example?
C - Difference between char array and char pointer.
In C language, a char array and a char pointer are two different data types with some fundamental differences.
Char array: A char array is a fixed-size block of contiguous memory locations, each of which holds a single character value. At the time of its declaration, the array's size is fixed and cannot be changed.
Here is an example of a char array:
char my_array[10] = "Hello";
char pointer: A char pointer is a variable that holds the memory address of a char value or a sequence of char values. It points to a memory location where the character value or the string is stored. String literals and dynamically allocated blocks of memory can be used to initialize char pointers.
Here is an example of a char pointer:
char *my_pointer = "Hello";
Here are some key differences between char arrays and char pointers:
Char array | char pointer | |
---|---|---|
Size | Char arrays have a fixed size. | char pointers can point to strings of any size. |
Initialization | Char arrays must be initialized at the time of declaration. | char pointers can be initialized later. |
Assignment | Char arrays can be assigned to other char arrays of the same size. | char pointers can be assigned to other char pointers or to the memory address of a string literal or a dynamically allocated block of memory. |
Modification | Char arrays can be modified directly using array indexing. For example, we can modify the first character of a char array as follows: my_array[0] = 'h'; |
char pointers require pointer arithmetic to modify the string they point to. To modify the first character of a char pointer, we would need to use pointer arithmetic as follows: *(my_pointer + 0) = 'h'; |
Contribute your code and comments through Disqus.
C Programming: Tips of the Day
C Programming - How do you pass a function as a parameter in C?
Declaration
A prototype for a function which takes a function parameter looks like the following:
void func ( void (*f)(int) );
This states that the parameter f will be a pointer to a function which has a void return type and which takes a single int parameter. The following function (print) is an example of a function which could be passed to func as a parameter because it is the proper type:
void print ( int x ) { printf("%d\n", x); }
Function Call
When calling a function with a function parameter, the value passed must be a pointer to a function. Use the function's name (without parentheses) for this:
func(print);
would call func, passing the print function to it.
Function Body
As with any parameter, func can now use the parameter's name in the function body to access the value of the parameter. Let's say that func will apply the function it is passed to the numbers 0-4. Consider, first, what the loop would look like to call print directly:
for ( int ctr = 0 ; ctr < 5 ; ctr++ ) { print(ctr); }
Since func's parameter declaration says that f is the name for a pointer to the desired function, we recall first that if f is a pointer then *f is the thing that f points to (i.e. the function print in this case). As a result, just replace every occurrence of print in the loop above with *f:
void func ( void (*f)(int) ) { for ( int ctr = 0 ; ctr < 5 ; ctr++ ) { (*f)(ctr); } }
Ref : https://bit.ly/3skw9Um
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises