c++ - confusion of char *notes[] = {"Ab", "F#", "B", "Gb", "D"}; and char** -


i confused type of pointer definition:

char *notes[] = {"ab", "f#", "b", "gb", "d"};`.  

i understand notes here array of pointer char, understand notes' elements should addresses of char typed variables. wrong? how work?

#include<iostream> #include<string>  using namespace std;  int main() {     char *notes[] = {"ab", "f#", "b", "gb", "d"};     cout << *(char**)(notes+2); } 

also char** cast there , significance?

in sense, char *notes[] means notes[] array pointer char

it means nodes array of char*, i.e. array of character pointers.

notes[] 's elements should addresses of char typed variables.

c implicitly converts string literals (i.e. character sequences enclosed in double quotes) null-terminated c strings, , produces addresses of initial character pointers adding array. how array gets initialized.

here example of how data placed in memory:

address value character ------- ----- --------- 1000000    65 1000001    98 b 1000002    00 null terminator 1000003    70 f 1000004    35 # 1000005    00 null terminator 1000006    66 b 1000007    00 null terminator 1000008    71 g 1000009    98 b 1000010    00 null terminator 1000011    68 d 1000012    00 null terminator 

then array of pointers initialized follows:

notes = {1000000, 1000003, 1000006, 1000008, 1000011}; 

note: above layout example. string literals may not placed in memory back-to-back.


Comments