0

I started learning pointers in C. I understood it fine untill I came across the topic "Using Pointers to store character arrays". A sample program to highlight my doubt is as follows

#include <stdio.h>
main()
{
  char *string;
  string = "good"; 
  printf ("%s", string);
}

This prints the character string, i.e, good.

Pointers are supposed to store memory addresses, or in other words, we assign the adress of a variable (using the address operator) to a pointer variable. What I don't understand is how are we able to assign a character string directly to the pointer? That too without address operator? Also, how are we able to print the string without the indirection operator (*) ?

Abhijit Singh
  • 57
  • 2
  • 10
  • 1
    Possible duplicate of [How to store a string into a char pointer?](https://stackoverflow.com/questions/29486269/how-to-store-a-string-into-a-char-pointer) – ArunPratap Jul 03 '18 at 11:11
  • `string` contains the address of "good". The `%s` expects a pointer. – stark Jul 03 '18 at 11:14
  • 1
    Dennis was drunk and, after the tenth tequila, decided that arrays would be treated totally differently to other types, so spawning billions of bugs over the decades:( – Martin James Jul 03 '18 at 11:17
  • Tl;dr: strings are treated as a bit of a special case in C. – Lundin Jul 03 '18 at 11:23
  • @MartinJames: Remember that C didn't spring fully formed from Ritchie's head - he derived it largely from B, and wanted to retain B's array semantics without materializing the pointer those semantics required (see [this paper](https://heim.ifi.uio.no/inf2270/programmer/historien-om-C.pdf) for details). Honestly, a lot of C's weirdness can be traced back to B. – John Bode Jul 03 '18 at 13:28
  • Try python, broh. – purec Jul 03 '18 at 15:12

4 Answers4

4

A literal string like "good" is really stored as a (read-only) array of characters. Also, all strings in C must be terminated with a special "null" character '\0'.

When you do the assingment

string = "good";

what is really happening is that you make string point to the first character in that array.

Functions handling strings knows how to deal with pointers like that, and know how to loop over such arrays using the pointer to find all the characters in the string until it finds the terminator.


Looking at it a little differently, the compile creates its array

char internal_array[] = { 'g', 'o', 'o', 'd', '\0' };

then you make string point to the first element in the array

string = &internal_array[0];

Note that &internal_array[0] is actually equal to internal_array, since arrays naturally decays to pointers to their first element.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

"cccccc" is a string literal which is actually the char array stored in the ReadOnly memory. You assign the pointer to the address of the first character of this literal.

if you want to copy string literal to the RAM you need to:

char string[] = "fgdfdfgdfgf";

Bare in mind that the array initialization (when you declare it) is the only place where you can use the = to copy the string literal to the char array (string).

In any other circumstances you need to use the appropriate library function for example.

 strcpy(string, "asdf");

(the string has to have enough space to accommodate the new string)

0___________
  • 60,014
  • 4
  • 34
  • 74
1

What I don't understand is how are we able to assign a character string directly to the pointer? That too without address operator?

When an array is assigned to something, the array is converted to a pointer.

"good" is a string literal. It has a array 5 of char which includes a trailing null character. It exists in memory where write attempts should not be attempted. Attempting to write is undefined behavior (UB). It might "work", it might not. Code may die, etc.

char *string; declare string as pointer to char.

string = "good"; causes an assignment. The operation takes "good" and converts that array to the address and type (char*) of its first element 'g'. Then assigns that char * to string.

Also, how are we able to print the string without the indirection operator (*) ?

printf() expects a char * - which matches the type of string.

printf ("%s", string); passes string to printf() as a char * - no conversion is made. printf ("%s",... expects to see a "... the argument shall be a pointer to the initial element of an array of character type." then "Characters from the array are written up to (but not including) the terminating null character." C11 §7.21.6.1 8.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

Your first question:
What I don't understand is how are we able to assign a character string directly to the pointer? That too without address operator?

A character string literal is a sequence of zero or more multibyte characters enclosed in double-quotes, for e.g. "good".

From C Standard#6.4.5 [String literals]:

...The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence.....

In C, an expression that has type array of type is converted to an expression with type pointer to type that points to the initial element of the array object [there are few exceptions]. Hence, the string literal which is an array decays into pointer which can be assigned to the type char *.

In the statement:

string = "good";

string will point to the initial character in the array where "good" is stored.

Your second question:
Also, how are we able to print the string without the indirection operator (*) ?

From printf():

s
writes a character string
The argument must be a pointer to the initial element of an array of characters...

So, format specifier %s expect pointer to initial element which is what the variable string is - a pointer to initial character of "good". Hence, you don't need indirection operator (*).

H.S.
  • 11,654
  • 2
  • 15
  • 32