简体   繁体   中英

Regarding char pointer initialization

Case 1:

char *str = {"one", "two", "three"};

puts(str); // displays one

Q1: How can we store an array of string in char* . How does it work internally? How can we display two.

Case 2:

According to my understanding str is a pointer to a character. So, it will store an address of a character. Now it depends upon how we print it.

char *str2 = "Hello";

printf("%c", *str2); // display H     [displays the char pointed by the string]

printf("%s", str2); // display Hello [basically looks for '\0' to stop printing]

Q2: How do we differentiate str (above) and str2 ?

Q3: pointer variable str2 is stored on stack. is it? Where is "Hello" stored?

Case 3:

char *str3 = malloc(sizeof(char));

str3 = "Yellow";

puts(str3);

Q4: str3 is on stack and it points to block of memory (1 byte) in heap. Is it correct? Still I am able to store "Yellow" . How?

I know it is basic, but some confusion persists in my head that is not giving me a clear difference while thinking in terms of memory. Please advise and feel free to correct me.

How can we store an array of string in char*. How does it work internally? How can we display two.

You can't store anything but addresses in a char*. You can however allocate an array of char* pointers, each pointing at a string. When you do so, you should declare the array as const , because you are not allowed to modify a string literal through a pointer.

const char* str[3] = {"one", "two", "three"};

for(int i=0; i<3; i++)
  puts(str[i]);

How do we differentiate str(above) and str2?

With different syntax, they are different things. Your first example only pointed at one single string "one". The other two strings in the initializer list were discarded by the compiler. You should have gotten a warning for that.

pointer variable str2 is stored on stack. is it?

Yes.

Where is "Hello" stored?

In a read-only memory section of your program, often referred to as .rodata (read-only data). Again, this is why you should declare all such pointers to string literals as const .

str3 is on stack and it points to block of memory(1 byte) in heap. Is it correct?

Yes.

Still I am able to store "Yellow". How?

You aren't storing anything. The only thing you can store in a pointer is an address. So when you write str3 = "Yellow"; you tell the program: forget about that heap memory I just allocated, lets point as this read-only string instead. So you have created a bug, a memory leak, because nothing is pointing at that heap segment any longer.

Case 1:

The usuall/normal usage is

char *str[] = {"one", "two", "three"};

puts(str[0]); // "one"
puts(str[1]); // "two"

Case 2:

Q3: pointer variable str2 is stored on stack. is it? Where is "Hello" stored?

If str2 a local variable, yes, it is stored on stack; but if it is a global variable, it will be stored in data segment. "Hello" is stored in some (readonly) data segment.

Case 3:

Q4: str3 is on stack and it points to block of memory(1 byte) in heap. Is it correct?

Yes.

Still I am able to store "Yellow". How?

You did not store "Yellow" to the memory pointed to before the assignment. In char *str3 = "Yellow"; , str3 will get the address of "Yellow" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM