There are two arrays:

  • char *str
  • char* str[]

By its principle, this is just a one-dimensional array, but at the same time, I cannot add any values โ€‹โ€‹to the second. Question: what is the difference between them then? Why declare them exactly in this form and what is a plus for it?

Update enter image description here

  • one
    You have an array of constant strings. You need const char* strs[] = {"Hello", "world"}; - ฿Š฿š฿ค฿˜
  • @Arhad. so it works. thank. pancake, I've been working in java for three years already, but I can't figure out the C ++ arrays. - raviga
  • It's not about arrays. In C and C ++, a constant (immutable, final) can be any variable. In the case of pointers, constancy can be hung not only on the pointer itself, but also on access to what it points to. So we declare an array of pointers as const char * strs[] , that is, not a constant array of pointers to constant strings. - ฿Š฿š฿ค฿˜
  • We do this because any string constant (of the form "..." ) has the type const char[...] , that is, a constant array. This is where you got the initial error due to the attempt to assign a constant to where it is permissible to change values. - ฿Š฿š฿ค฿˜
  • one
    Not. This will be const char * const str[] - the pointer asterisk divides the ad into two parts. The left one refers to the specified type, and the right one to the indicating type (that is, directly to the pointer). By the way, thanks to this, the const char * foo and char const * foo expressions are completely similar. And another point: the spaces around the asterisks do not matter, they are purely for readability. - ฿Š฿š฿ค฿˜

4 answers 4

  • char* str[] (with empty square brackets) - when used as an argument to a function when it is declared, this is the same as char **str .

    How to interpret this matter depends solely on the logic of the code:

    • It can be a pointer to an array of pointers (then you can do something like char *foo = str[16]; ). Moreover, the elements of the array can indicate both the beginning of null-terminated strings and individual symbols stored somewhere else.

    • It may simply be a pointer to a char* variable, allowing you to modify this variable itself from wherever we have passed this pointer.

    • This may be a two-dimensional array of type char . Such an array is called jagged and, unlike the traditional one, allows you to store a different number of elements in each row (if the first index indicates the row number) or column (if the first index indicates the column number)

  • char* str[N] (with a certain constant in brackets) is already a full-fledged array of fixed length, which stores in itself N elements of type char * .

    However, in this case there are three important points:

    1. This only works when declaring variables. If you try to specify a similar construct as an argument to a function when it is declared, the contents of the square brackets will be ignored, and you will get the first case.

    2. If you combine array declaration with its filling ( char* str = {...} ), the constant N can be omitted, then its value will be calculated automatically.

    3. In C ++, the value of N must be exactly a constant, otherwise you will get a compilation error. Variable-length arrays are allowed only in C no earlier than C99.

  • "char str [] (with empty square brackets) is the same as char * str" - only if it is a parameter in a function. In other cases, this is a full-fledged array of fixed length. The treatment in each case is always unambiguous. - VTT
  • @VTT, thanks for the clarification, corrected. - ฿Š฿š฿ค฿˜

The first is a pointer to char , the second is an array of pointers to char . Well, or, roughly speaking - the first is a C-line, the second is an array of pointers to these lines (but not the data itself).

Like that:

 char * str; str = new char[20]; strcpy(str, "Hello, world\n"); cout << str; delete[] str; const char* strs[] = { "Hello", "world" }; for(auto s: strs) cout << s << endl; 
  • So I tried to add elements in the same way as in the example below and I have a compilation error. Added screen to update. At the same time I tried something like: const char *str = "hello"; char* strs[5]; strs[0] = str; const char *str = "hello"; char* strs[5]; strs[0] = str; . But still a mistake. - raviga
  • roughly say it turns out then a two-dimensional array? - raviga
  • Yes, if roughly :) - Harry
  • For your Update , do, for example, like this: char* strs[] = {(char*) "Hello", (char*)"world" }; . Or const char* strs[] = { "Hello", "world" }; Although many compilers don't care - see, for example, ideone.com/VmLMAl - Harry

In the first case, you just have a pointer to char , but the second case is more interesting and is not at all equal to the first.

char *str[] is an array of pointers to char . Tobish it can be represented as an array of strings (which is not quite true, but in general it can be so). Tobish if you want the pointer from the array to point to the line you need, you need something like:

 char alfa[] = "alfa"; char beta[] = "beta"; char *str[2]; str[0] = alfa; str[1] = beta; 
  • if you can create an array like char *alfa = ... and char alfa[] =... , it turns out that in the case of arrays [] same as the pointer * ? - raviga
  • @DimaKhodan is not quite, a bit more complicated here: the fact is that char *alfa is obviously a pointer, but char alfa[] is an array of characters (this is why you cannot directly specify a pointer to a literal, but you can set an array). In this case, char alfa[] implicitly cast to a pointer to char , so alfa can be used as char * const . IMPORTANT!! If the pointer (not marked as * const ) can be changed, then with the array this is not a roll, since the array is implicitly reduced to char * const . - Andrej Levkovitch

The first is a pointer to char , and the second, depending on the context, can be either an array of pointers or a pointer to a pointer (if this is the type of the function argument).