Using a pointer to a char array in C ++

I rarely relate to coding and have some problems understanding how pointers work in combination with arrays (which I myself understand).

I realized that it is possible to create an array of pointers like:

#include <iostream>
using namespace std;

int main() {
    int i;
    int *pArr[10];
    pArr[0]=&i;
    return 0;
}

In the tutorial, I found the following code:

#include <iostream>
using namespace std;

int main() {
    char *names[4] = {
        "name A",
        "name B",
        "name C",
        "name D"
};

for (int i = 0; i < 4; i++) {
    cout << names[i] << endl;
}
return 0;
}

Why is it that I can assign multiple characters or say a string like "name A" to a pointer that should point to a char.

Do I A:

Only be able to assign a char address to each of these 4 pointers that I created.

And B:

Only be able to assign a pointer to one letter (a char) to each.

I hope someone can help to some extent eliminate my confusion.

+4
4

, , .

"name A", :

const char hidden0[] = {110, 97, 109, 101, 32, 65, 0}; // "name A"

. - .

, :

const char *names[4] = {
    &hidden0[0],
    &hidden1[0],
    &hidden2[0],
    &hidden3[0]
};

hiddenK - , . , name[].

+6

C ( ++) (, "name A") const char*. - . . ,

char *names[4] = {
    "name A",
    "name B",
    "name C",
    "name D"
};

, names.

+3

4 char.

A "string litteral" char. - char. , .

? char * const char *? , SO

+2

char * . , "mystring" , char * "m" "mystring" .

cout , char *, , , , ( 0, "0" ).

, char *, . 1 , "mystring" , "ystring". , , a char * , .

+1
source

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


All Articles