How to Dereference a CHAR Pointer in C?

Before understanding how to dereference a char pointer in C, it is vital to know what dereferencing is and how it works. 

Also Read: How to dereference a void pointer in C

What is Dereferencing?

In C, dereference is done using an indirection pointer, i.e., * with the pointer variable. When you wish to access the data/value in memory that the pointer refers to – the contents of the address with that integer index – you dereference it. Dereferencing is accessing or modifying data stored at a pointer-referenced memory address.

How to Dereference a CHAR Pointer in C

Any operation on the dereferenced pointer immediately affects the value of the variable to which it references. A char pointer’s dereferencing code is similar to an int pointer.

Also Read: How Dangling Pointer in C affects your Programming

The first character of a char or the beginning of an array of strings may be referred to using a char reference to a string in C. If you want to know what character is kept at each place, use the asterisk * operator.

Also Read: What is Double Pointer in C

As an illustration, think about how a word or string might be stored in C using the char data type. We can build an array of characters to store several phrases in a single char variable instead of the single character that char can hold.

Let’s look at the steps below to see how to dereference a char pointer in C.

Declaring the Char variable

Individual characters, such as ‘x,” y,’ and ‘z,’ can be allocated to char variables and are signified by a single quote mark in C. We can also use an array to store a string and insert data in C.

Syntax: Char str = ‘a’;

After declaring the char variable, i.e., str, we assign an individual character, i.e., ‘a’, to it.

Ways to assign a Char/string to a Variable

We may give a character a char reference by employing a single quotation mark.

You must first establish a one-dimensional array before you can save a string in it. We can use the following syntax for dereferencing a char pointer across an array. We save an array of characters in the char array and then dereference the array variable.

Code:

Char str[5] = {‘H,” e’, ‘l,’ ‘l’, ‘o’};

We can assign value to the char variable using the method of declaring an array, i.e., inside the curly bracket or declaring an array of defined value

OR

Char str[5];

str[0] = ‘H’;

str[1] = ‘e’;

str[2] = ‘l’;

str[3] = ‘l’;

str[4] = ‘o’;

Otherwise, declare the char array first and assign the value index-wise.

 It is possible to specify the characters in a string in several places, such as when an array is declared, but it is also possible to set the value of an array using a string literal. The character array’s value is set using a literal string rather than individually initializing each character. Double quotation marks around a group of characters define a string literal.

Code:

Char *str = “Hello”;

Using a string literal, i.e., Hello, will work similarly to an array with a null character. The compiler appends a null character to the end of the string if it is not explicitly declared.

The value in this step is present somewhere in the memory area whose address is allocated in the str variable, and dereferencing str, i.e., *str = “Hello World,” returns the value stored in that address.

Putting in place a Char Pointer

Making a character reference to a string in C will allow you to locate the beginning of the character array. The string’s starting address, represented by this pointer, will lead us to the string’s first character. To get the value of the string, we may dereference this address.

Code:

Char *str = “Hello”;

Char *ptr = str;

Using a different pointer that contains the address of the string variable, which saves the string address, we are dereferencing the string value in this case. Said, *ptr is gaining access to the value kept in the str, which includes the text “Hello.”

Accessing values

When we point a char array to it, we give it the array’s base address. In C, dereferencing the pointer variable with the asterisk * symbol returns the character existing at the address.

As our string ends with a null ‘\0’ character, we can use this fact to automatically increment the pointer value and output each character until the pointer points to a null character using a while loop rather than manually incrementing the pointer to extract the value of the string.

Code:

Char *str = “Hello”;  //String Literal

Char *ptr = str; //char pointer

//Creating a while loop to print values till Null Character i.e. /0 is not found

While (*ptr != ‘\0’){

//The current character is not /0, so we will print the character

             printf(“%c”, *ptr);

//Move to the next character

             ptr++;

}

return 0;

}

After declaring the string literal and char pointer storing the value. Use a while loop; you can dereference the pointer with a string literal or an array till the null character is found. 

The current character is not ‘\0’, so we will print the character in the case of individual characters; use code line 15 to get the value printed.

Output

‘Hello’ will be printed after the successful compilation of the code.

The array str’s starting position is shown using a character pointer created in C. The value included in the array is shown using a while loop until the value at the location denoted by ptr is not null, indicating that we have not reached the end of the text.

The current character is printed before moving the ptr pointer to the following location. The loop ends whenever we come across the null character, which indicates the end of the string.

Conclusion

Here, we declare the char pointer variable in the above code using an array, a single character, and string literals. Following the declaration, the pointer stores the address of the value, implying that the asterisk pointer is the same as the value stored in the variable, i.e., *ptr is the same as str. We may access the string using methods such as while loops or manually raise the address value in other circumstances.

If you enjoyed reading this post and found it informative, check out more on the related subject and let your friends know about this.

Greetings, My name is Rumi Sadaf. I work as both a content writer and a programmer. In essence, I explain what I know and aid others in understanding programming concepts. Happy Viewing.

Leave a Comment