Home > Back-end >  The cycle of small problems about pointer
The cycle of small problems about pointer

Time:12-24

 
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 coincidence
You'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:

reference ctrigger reply: 3/f
two code didn't end the for loop, is wrong.

Do you think * p1 is not the end mark?

CodePudding user response:

quoted zhao 4, 4/f, the teacher's reply:
Quote: refer to the third floor ctrigger response:
two code didn't end the for loop, is wrong.

Do you think * p1 is not the end mark?

I said is the Lord, not to say to you, your answer is correct,

CodePudding user response:

reference 5 floor ctrigger reply:
Quote: refer to 4th floor 4 teacher zhao response:
Quote: refer to the third floor ctrigger response:
two code didn't end the for loop, is wrong.

Do you think * p1 is not the end mark?

I said is the Lord, not to say to you, your answer is correct,

Main problem is not out at the end of the no sign, but no explicit initialization for the fifth element behind the sixth element corresponding to the values in the memory, even if they don't statement sixth element, program to read sixth element seams and behind each element, so will read each element behind the sixth element and the corresponding value of memory, as for when can encounter happened to have zero value, that is not necessarily,

CodePudding user response:

This is not a small problem, overflow never is a small problem oh,
With * p as a condition of the end, to make the last value is 0, so that the end of cycle
Can do it:
The first
,2,3,4,5,0 int a [6]={1}
The second
Char a [6]={' 1 ', '2', '3', '4', '5', '\ 0'}

Also, suggest a good habit, pointer after use, assign a value to it NULL
Is at the end of the code and a line of p=NULL
This can be formed in the largely avoid "wild pointer"
  • Related