Do I understand correctly what all these pointers point to?

I created a program that creates three c-lines, and then an array of pointers to the size of the number of c-lines that I created (3), with each pointer in the array pointing to the first character of each of the three lines, and then finally a pointer to an array of pointers (which will point to the first pointer in the array).

Here is the code:

    int main() {
    char arr1 [] = "hello";
    char arr2 [] = "dear";
    char arr3 [] = "world";

    char* p1[3];
    *p1 = arr1;
    *(p1+1) = arr2;
    *(p1+2) = arr3;
    char** ptr = p1;
}

However, when I do something like std::cout << *ptr << std::endl;, I expect it to simply output the ADDRESS of the first pointer in the array of pointers. However, in fact, he displays the entire word "hello", which seems strange to me. I would expect the address, and if I **ptroutput it, I would expect it to simply print the letter "h"(which it really does) Am I misunderstanding something? The same thing if I did std::cout << *(ptr+1).. I would expect the address of the second pointer in an array of pointers p1, but in fact it gives the full word "dear". I was so lost .. I thought that dereferencing ptrwould give me an address p1, which by dereferencing it twice, would give me a value inside the address p1.

+4
source share
3 answers

, void:

cout << (const void*) *ptr << endl;

char * - C, :

std::ostream& operator << (std::ostream&, const char *);

. .

+3

"", , , << operator.

std::cout << *ptr << std::endl;

std::cout << arr1 << std::endl;

, < :

ostream& operator<< (ostream& os, const char* s);

. arr1 const char* s. s C '\ 0'

+3

: char * - ?

, ,

ostream& operator<<(ostream& o, const char* str);

c-string, void*, , . , . , , - , , - ( ):

int main() {
char arr1 [] = "hello";
char arr2 [] = "dear";
char arr3 [] = "world";

char p1[3][10];
p1[0] = arr1; //or *(p1+1) = arr1
p1[1] = arr2; //or *(p1+2) = arr2
p1[2] = arr3; //or *(p1+3) = arr3
char** ptr = p1;
}

10, . :

hello\0----dear\0-----world\0----
^          ^          ^
|          |          |
|          |          arr3,ptr[2]
|          arr2,ptr[1]
*ptr/ptr[0], arr1    

- , , 10 char p1[3][10];. , , . , malloc() , , :

char* a = (char*)malloc(10*sizeof(char));
char* b = (char*)malloc(10*sizeof(char));
char* c = (char*)malloc(10*sizeof(char));
strcpy(a, "hello");
strcpy(b, "dear");
strcpy(c, "world");
char** ptrs = (char**)malloc(3*sizeof(char*)); //3 pointers, ie: char* ptr[3];
ptrs[0] = a;
ptrs[1] = b;
ptrs[2] = c;

:

ptrs:---|, --|, ------|
        v    v        v
        a-\  b-----\  c----\
          v        v       v
         "hello"  "dear"  "world"

..: , , , . , ptrs a, b c, , , "", "" "". , ptrs (*ptrs), a (a char*, a char**), "". cout, forementioned operator<< c- , , . -, , , , *ptrs ptrs[0], , , .

And I hope I didn’t completely screw it up, giving you fictitious information, if someone sees something wrong or if I am completely mistaken, tell me so that I can delete it, as others did x). I also hope this made it clearer in some way, please ask something that is unclear.

+1
source

Source: https://habr.com/ru/post/1502964/


All Articles