Recently, I was interested in arrays and pointers, so I prepared char type arrays and pointers and tried various things, and summarized examples of "can be handled in the same way" and "cannot be treated in the same way".
Postscript: As you pointed out in the comments, this time we are talking about the char type.
Finally, I will show you the whole thing, but think of the code shown below as a series of codes in order from the top.
The compiler is gcc 7.3.0.
Both of the following are possible codes.
char array[4] = "ABC"; //Yes
char* pt = "DEF"; //Yes, const modification recommended
The array is explicitly stated to have 4 elements, but you can write char array [] =" ABC "
without doing so.
In that case, the number of elements is 4, and the last element is '\ 0'
(null character).
The pointer puts the string literal " DEF "
in memory and initializes it to point to it. This string literal is basically not guaranteed to be rewritten.
(For example, I'll introduce it later in this article, but I couldn't change the content with strcpy ()
)
There was also a way to write initialization in the array as shown below, but it was not possible with pointers.
char array2[4] = {'A','B','C','\0'}; //Yes
char* pt2 = {'D','E','F','\0'}; //Impossible
Postscript: It seems that it is possible by doing the following. This does not point to a string literal, but an array entity is created and its contents can be rewritten.
char* pt2 = (char[]){'D','E','F','\0'}; //Yes, an array entity is created
//Almost the same as below
char noname[]={'D','E','F','\0'};
char* pt2=noname;
You can assign to the elements of an array, but you cannot assign to an array. You can assign it to a pointer.
array = pt; //Impossible
pt = array; //Yes
The contents of the array can be rewritten using strcpy ()
, and the address value stored in the array does not change.
On the other hand, the pointer can change the character string pointed to by pt =" character string "
, but the address value stored in pt
also changes.
It seems that "JKL" is stored in another area and pointed to while keeping the "DEF" at the time of initialization in the memory, but I do not know the exact thing.
By the way, ʻarray =" character string "could not be created in an array, and
strcpy ()could not be created in a pointer initialized with
=" character string "`.
char array[4] = "ABC";
array = "GHI"; //Impossible
strcpy(array, "GHI"); //Yes
char* pt = "DEF";
strcpy(pt, "JKL"); //Impossible
pt = "JKL"; //Yes
Arrays cannot store strings longer than the number of elements, even if you use strcpy ()
.
On the other hand, the pointer was able to point to a string that exceeds the length of the first initialized string. Unlike an array, an area is newly allocated in another place to store a string literal and point to it, so the size of the area allocated first is irrelevant.
Curiously, the pointer pointed to " DEF "
had the same stored address value, including immediately after initialization. It seems that there is only one "DEF" in the memory.
pt = "DEFGHI";
printf("pt\t\"%s\": %p\n", pt, pt);
pt = "DEF";
printf("pt\t\"%s\": %p\n", pt, pt);
char* pt3 = "DEF";
printf("pt3\t\"%s\": %p\n", pt3, pt3);
output
pt "DEFGHI": 0x100403033
pt "DEF": 0x100403010
pt3 "DEF": 0x100403010
Both arrays and pointers can be accessed by subscripts or by pointer operations and indirect operators.
printf("2nd and 3rd characters of array\n");
printf("%c", array[1]);
printf("%c", *(array + 2));
printf("\n");
printf("2nd and 3rd characters of pt\n");
printf("%c", *(pt + 1));
printf("%c", pt[2]);
printf("\n");
output
2nd and 3rd characters of array
HI
2nd and 3rd characters of pt
EF
This is the whole code that compiles and its output. The impossible part is commented out.
sample.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(void){
char array[4] = "ABC"; //Yes
char array2[4] = {'A','B','C','\0'}; //Yes
printf("array\t\"%s\": %p\n", array , array);
char* pt = "DEF"; //Yes
// char* pt2 = {'D','E','F','\0'}; //Impossible
printf("pt\t\"%s\": %p\n", pt, pt);
char* pt2 = (char[]){'D','E','F','\0'};
printf("pt2\t\"%s\": %p\n", pt2, pt2);
// array = pt; //Impossible
pt = array; //Yes
pt = "DEF"; //Return to the state at the time of initialization
// array = "GHI"; //Impossible
strcpy(array, "GHI"); //Yes
printf("array\t\"%s\": %p\n", array, array);
// strcpy(pt, "JKL"); //Impossible
pt = "JKL"; //Yes
printf("pt\t\"%s\": %p\n", pt, pt);
pt = "DEFGHI";
printf("pt\t\"%s\": %p\n", pt, pt);
pt = "DEF";
printf("pt\t\"%s\": %p\n", pt, pt);
char* pt3 = "DEF";
printf("pt3\t\"%s\": %p\n", pt3, pt3);
printf("2nd and 3rd characters of array\n");
printf("%c", array[1]);
printf("%c", *(array + 2));
printf("\n");
printf("2nd and 3rd characters of pt\n");
printf("%c", *(pt + 1));
printf("%c", pt[2]);
printf("\n");
return 0;
}
output
array "ABC": 0xffffcc00
pt "DEF": 0x100403010
pt2 "DEF": 0xffffcc04
array "GHI": 0xffffcc00
pt "JKL": 0x10040302f
pt "DEFGHI": 0x100403033
pt "DEF": 0x100403010
pt3 "DEF": 0x100403010
2nd and 3rd characters of array
HI
2nd and 3rd characters of pt
EF
I hope it will be a reference for how to handle arrays and pointers.
For pointers, I tried various pointer passing and pointer operations in C language may also be helpful.
Recommended Posts