简体   繁体   中英

C Language Pointer Used With Arrays

I compiled and ran the following code and the results too are depicted below.

#include <stdio.h> 
int main(void) {

    char *ptr = "I am a string"; 

    printf("\n  [%s]\n", ptr);

    return 0; }

** [I am a string]**

I want to understand how a string has been assigned inside a pointer char. As per my understanding the pointer can hold only an address, not a complete string. Here it is holding one whole sentence. I do not understand how being a pointer allows it to behave such way.

If I change the following line of code in the above example,

printf("\n  [%c]\n", ptr); 

It does not print one single charactor and stop. What it does is that it prints out an unrecognised character which is completely out of ASCII table. I do not understand how that too is happening. I would appreciate some light shred on this issue.

As per my understanding the pointer can hold only an address, not a complete string

char *ptr = "I am a string";

Is a string literal the string is stored in the read-only location and the address in which the data is stored is returned to the pointer ptr .

It does not print one single charactor and stop. What it does is that it prints out an unrecognised character which is completely out of ASCII table. I do not understand how that too is happening

ptr is a pointer and using wrong format specifier in printf() lead to undefined behvaior.

With %s if you provide the address where the string is stored the printf() prints out the whole string

A pointer does not hold a string, it points to a string. (Easy to remember, it's called a "pointer", not a "holder"). To see the difference, write your postal address on a yellow sticky note. Does this piece of paper hold you? No, it points to you. It holds your address .

Pointers are computer equivalent of postal addresses (in fact things that pointers do hold are called addresses). They don't hold "real things" like strings, they tell where "real things" live.

Back to our string, the pointer actually points to the first character of the string, not to the string as a whole, but that's not a problem because we know the rest of the string lives right next to the first chsracter.

Now "%s" as a format specifier wants a pointer to the first character of a string, so you can correctly pass p to printf . OTOH %c wants a character, not a pointer, so passing p in this case leads to undefined behavior.

So how come we can say things like char* p = "abc" ? String literals are arrays of characters, and an array in most cases decays into pointer to its first element. Array-to-pointer decay is another confusing property of C but fortunately there is a lot of information available on it out there. OTOH `char p = "abc" is not valid, because a character is not an array (a house is not a street).

Also

char *ptr = "I am a string";

automatically inserts a null character at the end. So when you do a printf with %s format specifier, it starts from the address of the string literal and prints upto the null character and stops.

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