Int a [5]={1, 2, 3, 4, 5};
Int * p=a;
for (; * p; P++) {
Printf (" % d, % d \ n ", * p, p);
}
Like this, it is clear that the output of
1, 6684160
2, 6684164
3, 6684168
4, 6684172,
5, 6684176
When I change the data structure to char, would be a little problem
Char a [5]={' 1 ', '2', '3', '4', '5'};
Char * p=a;
for (; * p; P++) {
Printf (" \ n % c, % d ", * p, p);
}
The results of the output even after five and garbled
1, 6684179
2, 6684180
3, 6684181
4, 6684182,
5, 6684183
6684184
? 6684185
E, and 6684186
Why the output after 5? Write a for loop, * p mean value is 0, pointer beyond an array subscript 5, why continue to point to a [4] next address? But never this kind of situation in an int type, why is this?
CodePudding user response:
The first piece of code output just coincidenceYou've been crossing the line
Two pieces of code are wrong
No end 0
Cycle check 0 doesn't make sense
CodePudding user response:
# include & lt; stdio.h>
Int main () {
Int a1 [6]=,2,3,4,5,0 {1};
Int * p1=a1;
for (; * p; P1 + +) {
Printf (" % d, % 0 x p \ n ", * (p1, p1);
}
Char a2 [6]={' 1 ', '2', '3', '4', '5', '\ 0'};
Char * p2=a2;
for (; * (p2); The p2 + +) {
Printf (" % c, 0 x % p \ n ", * (p2), p2);
}
return 0;
}
//1, 0 x0135fbe4
//2, 0 x0135fbe8
//3, 0 x0135fbec
//4, 0 x0135fbf0
//5, 0 x0135fbf4
//1, 0 x0135fbfc
//2, 0 x0135fbfd
//3, 0 x0135fbfe
//4, 0 x0135fbff
//5, 0 x0135fc00
//
Actually computer after each byte of physical memory has values and is read/write, never because of the so-called new, delete or malloc, free and is created and destroyed, the difference only lies in the operating system memory management module can find when you read and write and whether to take appropriate action, and the granularity of the operating system manages memory rather than bytes page, a page is usually 4 KB,
CodePudding user response:
Two pieces of code didn't end the for loop, is wrong,CodePudding user response: