# include<stdio.h>
int main (void) {
char a [] = "012345", * p = a + 1;
printf ("% d \ n", a);
(* p) ++;
printf ("% c \ n", * p);
}
This is a rudimentary question about the C language, but I don't understand why the output is -491984928 and 2.
Please tell me the following questions.
①Do you think a is an array? However, when I displayed it with printf, I didn't know where the numbers came from.
② What is the value in * p, which is 1 in the array in * p = a + 1;
③ I don't understand the meaning of (* p) ++;* p ++;Why does the value change?
-
Answer # 1
-
Answer # 2
printf ("% d \ n", a);
displays the address of a (those that do not know where the numbers come from).
Try changing toprintf ("% p \ n", a);
-
Answer # 3
①
char a [];
means an array of char type.
a [0] is char, a [1] is char, a [] 2 is char ...
Therefore, ifprintf ("% c", a [0]);
is displayed, '0' which is the contents ofa [0]
is displayed.
However,a
is not char, but indicates the start address of a [].
Sincechar a []
is declared, the type ofa
ischar *
.
The start address ofa []
is the same as the address&a [0]
ofa [0]
.
printf ("% d \ n", a);
andprintf ("% d \ n",&a [0]);
have the same result.②
a
is of typechar *
, soa + 1
is added toa
by 1 Byte (one char) Displayed. That is the address ofa [1]
.
Related articles
- about c language pointers, lists, and stacks
- about float type operation of c language
- about c language structure, typedef
- about multiplication of c language array
- about c language pointers
- (c language) i want to output the bbox coordinate data detected by yolo v3 to a txt file
- about c language character string operation
- about the code using the c language sleep function
- about c language standard input
- about c language line breaks
- about the comparison function of c language bsearch function
- c language output of numbers in the specified range and number of lines
- about data race of shared variables in c language threads
- c language: about the location of random number expressions
- about how to store a c language txt file in an array
- about the c language scanf function
- about initialization of c language array
- about how to store a c language txt file in an array
- about the problem of c language recursion
- about c language programs
Yes.
However, depending on the context, it may behave as a pointer to the first element.
p = a + 1 is the same as p =&a [0] +1.
Use the format specifier% s to output a string.
The next address after the start address of the array.
Also, since char * is a p type, it is p = a + 1 rather than * p = a + 1.
* p ++ is the same as * (p ++).
AddedUsing a service that visualizes the behavior of code like this makes it a little easier to harden the image.
If you jump to the link below and wait for a while, the screen will change as shown in the image.
You can proceed one step at a time with the "Next" button.
Visualize Python, Java, JavaScript, C, C ++, Ruby code execution