S
Slain
I am a beginner and have some confusion with respect to pointers and
strings. It seems that the pointers with dealing with integer arrays
behave differently, as opposed to strings. Can some one explain me the
difference?
Sample Program:
int main()
{
int array[]={1,2,3,4,5};
char array1[]={"Name is Max"};
int *ptr;
char *ptr1;
ptr=array;
ptr1=array1;
cout<<"The array is "<<array<<endl;
cout<<"ptr is "<<ptr<<endl;
cout<<"*ptr is "<<*ptr<<endl;
cout<<"The array is "<<array1<<endl;
cout<<"ptr1 is "<<ptr1<<endl;
cout<<"*ptr1 is "<<*ptr1<<endl;
}
Output:
The array is 0xbfe09380
ptr is 0xbfe09380
*ptr is 1
The array is Name is Max
ptr1 is Name is Max
*ptr1 is N
I can understand that in both the cases, *ptrx points to the element
in it's address location. But why in the case of "ptr1", when I am
using a string ptr1 refers to "Name is Max" and not the address of the
"array1', like it did with "array".
Thanks
Manny
strings. It seems that the pointers with dealing with integer arrays
behave differently, as opposed to strings. Can some one explain me the
difference?
Sample Program:
int main()
{
int array[]={1,2,3,4,5};
char array1[]={"Name is Max"};
int *ptr;
char *ptr1;
ptr=array;
ptr1=array1;
cout<<"The array is "<<array<<endl;
cout<<"ptr is "<<ptr<<endl;
cout<<"*ptr is "<<*ptr<<endl;
cout<<"The array is "<<array1<<endl;
cout<<"ptr1 is "<<ptr1<<endl;
cout<<"*ptr1 is "<<*ptr1<<endl;
}
Output:
The array is 0xbfe09380
ptr is 0xbfe09380
*ptr is 1
The array is Name is Max
ptr1 is Name is Max
*ptr1 is N
I can understand that in both the cases, *ptrx points to the element
in it's address location. But why in the case of "ptr1", when I am
using a string ptr1 refers to "Name is Max" and not the address of the
"array1', like it did with "array".
Thanks
Manny