I am trying to loop an array of integers using pointer, but I get a strange values..
int nums[1] = { 1 };
int *p = nums;
while(*p != NULL) {
cout << " LOOPING, p is " << *p << endl;
p++;
}
When I am running, I am getting the next output:
LOOPING, p is 1
LOOPING, p is -858993460
LOOPING, p is 4454504
LOOPING, p is 3032019
Why I am get those strange values? I should see only “1”, because I am looping until I get NULL pointer, and on each loop I move the next pointer.
The comparison is wrong for 2 reasons:
*pis an integer, not a pointer likeNULLimpliesNULLis treated like a fancy 0, it won’t workYou could try: