just excersize from stroustrup: declare and initialize pointer of string array. I can do
char *test1[]={"ddd"}
but can't
char (*test)[] ={"dfsdf"}.
which is difference between these declarations and how initialize second ?
First is an array of pointers to the type char
.
Second is a pointer to array of type char
.
This small code snippet should be good to understand the difference:
#include<stdio.h>
#include<string.h>
int main()
{
char *test1[]={"ddd","aaa"};
printf("[%s]",test1[0]);
printf("[%s]",test1[1]);
char arr[]={"bbb"};
char (*test2)[] = &arr;
printf("[%s]",*test2);
return 0;
}
Output:
[ddd][aaa][bbb]
test1
is a array of pointers, each subscript of this array points to character string.
test1[0]
& test1[1]
allow you to obtain the content being pointed.
test2
is pointer to another array. Dereferencing the pointer *test2
gives you the array being pointed to.
You have created an array of pointers with the following code:
char *test1[]={"ddd"};
The code below is a pointer to an array. "ddd"
is implicitly an array of characters.
char *test1 = "ddd";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.