: 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;
p1[1] = arr2;
p1[2] = 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*));
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.