How to determine the end of an integer array when manipulating with integer pointer?
Posted
by AKN
on Stack Overflow
See other posts from Stack Overflow
or by AKN
Published on 2010-04-29T09:16:38Z
Indexed on
2010/04/29
9:57 UTC
Read the original article
Hit count: 246
Here is the code:
int myInt[] ={ 1, 2, 3, 4, 5 };
int *myIntPtr = &myInt[0];
while( *myIntPtr != NULL )
{
cout<<*myIntPtr<<endl;
myIntPtr++;
}
Output: 12345....<junks>..........
For Character array: (Since we have a NULL character at the end, no problem while iterating)
char myChar[] ={ 'A', 'B', 'C', 'D', 'E', '\0' };
char *myCharPtr = &myChar[0];
while( *myCharPtr != NULL )
{
cout<<*myCharPtr<<endl;
myCharPtr++;
}
Output: ABCDE
My question is since we say to add NULL character as end of the strings, we rule out such issues! If in case, it is rule to add 0 to the end of integer array, we could have avoided this problem. What say?
© Stack Overflow or respective owner