2D Arrays and Pointers - C

Just trying to really understand the arrays and pointers to C and the differences between them, and I am having problems with 2d arrays.

For a normal 1D array, this is what I found out:

char arr[] = "String constant";

creates an array of characters, and the variable arrwill always represent the memory created when it was initialized.

char *arr = "String constant";

creates a pointer to char, which currently points to the first index of the char array "String constant". A pointer may point elsewhere later.

char *point_arr[] = {
    "one", "two","three", "four"
};

creates an array of pointers, which then point to the char arrays of "one", "two", etc.

My question

If we can use both:

char *arr = "constant";

and

char arr[] = "constant";

then why can't i use:

char **pointer_arr = {
    "one", "two", "three", "four"
};

instead

char *pointer_arr[] = {
    "one", "two", "three", "four"
};

char **, , , " ". char**, , calloc, char *arr = "blah";. , , :

char **arr_pointer;

char *arr_pointer[];

.

+3
5

, { ... } .

char **arr_pointer , . , , char *arr = "constant";, , , .

+3

. FAQ :

char [] vs char *. char *[] vs char **.

+5

, ints, . , . ints , , .

int * ptr [] - , int ** ptr , .

int * arrptrs [2];
arrptrs [0] = (int *) malloc (sizeof (int) * 5);
arrptrs [1] = (int *) malloc (sizeof (int) 5);
, arrptrs. , arrptrs (int *), (int *)

, int ** ptr = arrptrs , * ptr - arrptrs, arrptrs [0] * (ptr + 1), arrptrs [1], * arrptrs [0] , arrptrs [0].

, , , .

+1

(char *pointer;) ; (char array[];) .

char **ptr2 , , , .

0

:

- , ...... , char, int, float double C.

, {.....}, , char ** arr_pointer "" (), {...} .

0

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


All Articles