C programming: char * const and const char *
Difference between char * const and const char *?
const char *: char* const is a constant pointer to a character array. It means that the pointer itself is a constant, and cannot be reassigned to point to another address, but the contents of the array it points to can be modified.
char * const: char* const is a C type that represents a constant pointer to a non-constant character. This means that the memory address pointed to by the pointer cannot be changed, but the value stored at that memory address can be changed.
Example const char *:
#include<stdio.h>
void print_string(const char *str) {
while (*str != '\0') {
printf("%c", *str);
str++;
}
printf("\n");
}
int main() {
const char *text = "Hello, World!";
print_string(text);
return 0;
}
In the above example print_string() takes a const char * parameter “str”, which means that the function promises not to modify the contents of the string pointed to by "str". The while loop in print_string() uses pointer arithmetic to traverse the string, printing each character one at a time.
In main() function, a const char * variable message is declared and initialized with the string literal "Hello, World!". print_string() is then called with message as the argument. Message is a constant string, so the function cannot modify it.
Example char * const:
#include <stdio.h>
int main() {
// create a constant pointer to a string
char * const text = "Hello, World!";
// this line would result in a compilation error,
// since myString is a constant pointer
// text = "Goodbye, World!";
printf("%s\n", text);
return 0;
}
Output:
Hello, World!
In the above example, "text" is a constant pointer to a string literal "Hello, World!". Since the pointer is declared as const, it cannot be reassigned to point to a different location. We will get an error if we uncomment the line that tries to reassign "text".
We have not declared the string as a const, so we can still modify the contents of the string that "text" points to.
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