简体   繁体   中英

Array of pointers to char arrays

I am confused about the following line of code:

char* words[] = { "aaa", "bbbb", "ccccc", "dddddd" };

The way I understand, each word is first stored and then each position of the array words will point then to the first character of each word. How are these strings stored? Is there dynamic allocation going on here or are these words stored on the stack?

If they are stored on the stack, in which way are they stored? For instance, if I print some of the contents of words as below:

#include <stdio.h>
int main () {
    char* words[] = { "aaa", "bbbb", "ccccc", "dddddd" };
    printf("\n\n(*words)[0] = %s", words[0]);
    printf("\n\n(*words)[0]+1 = %s", words[0]+1);
    return 0;
}

instead of printing aaa and bbbb , what I get is aaa and aa . I do not really understand what is the reason for this since, the way I see it, words[0]+1 should point to the string bbbb and not to the second character of aaa . What is going on here?

this is like..

在此输入图像描述

since words an array of character of pointers so each array index of words will hold the address of the string literal ,ie base address of the string literals if you will print

 printf("%s",words[0])// index 0 will print aaa.

The difference is because words[0]+1 is not the same as words[0+1] .

The former points to the second character in words[0] , while the latter points to the second word.

words[0] points to the first 'a' in "aaa".

words[0]+1 moves that pointer along by one character, so it comes to point at the second 'a'.

words[1] points at "bbbb"

words[1]+1 points "bbb", eg the second 'b' in "bbbb".

the way I see it, words[0]+1 should point to the string bbbb and not to the second character of aaa

No. words[0] is a char pointer itself - it's perfectly fine that you get the second character of "aaa" by adding one to it. What you want is words + 1 or &words[0] + 1 which will correctly be "bbbb".

Also, the strings themselves are allocated upon launching the executable and they're probably put in the data or bss section of the binary by the linker. Also, when you declare and initialize words , it'll be allocated on the stack as any other automatic array and its items will be assigned to pointers to the beginning of each string constant.

The literal strings are stored in static memory. Their actual location is implementation-dependent but literal strings are stored somewhere , typically in the data portion of the executable - this is neither dynamic nor stack allocation. Your array then contains pointers to these locations.

words[0]+1 should should point to the string bbbb and not to the second character of aaa.

This simply isn't how array indexing works. You index the array of strings with words[0] , now you have a string, and any operations apply to that string. You can't do arithmetic with array indices outside the subscript. To get to the string "bbbb" , you would use words[1] .

Both stack and heap space is dynamically allocated -- that is, they are allocated at run time. Is your compiled code dynamically allocated, on the heap or stack? Obviously neither. Storage for constants is similar to storage for code ... they are stored in the executable on disk and loaded into read-only memory. (Note for pedants: this is how things are done in a typical implementation; it is not mandated by the language standard.)

words[0] is the address of the first 'a' of "aaaa". Adding 1 to that address should surely yield the address of the second 'a' of "aaaa". The address of "bbbb" is way over at words[1] .

In the format of your printf you have "(*words)[0]", but that's different. *words is the same as words[0] . (*words)[0] is the same as **words , which is the first 'a' (not its address) of "aaaa". You would print (*words)[0] with %c , not %s .

words[0] returns the address of aaa. Adding 1 to that increments the address to point to the second a.

Do you mean to have words[0+1]?

That should give you what you are expecting.

char* words[] = { "aaa", "bbbb", "ccccc", "dddddd" };

All the 4 strings have static storage duration and are allocated prior to the program startup.

In the initializer, the arrays are converted to pointers to char and the words array is initialized with the pointer values.

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