Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pointers + dealing with integer arrays and strings

Reply
Thread Tools

Pointers + dealing with integer arrays and strings

 
 
Slain
Guest
Posts: n/a
 
      07-12-2007
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

 
Reply With Quote
 
 
 
 
Robert Bauck Hamar
Guest
Posts: n/a
 
      07-12-2007
Slain wrote:

> 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;


array decays into a pointer to its first value. As cout has a way to output
const void* (== void const*), but not int*, the pointer is implicitly
converted to const void*.

> cout<<"ptr is "<<ptr<<endl;


Here, you already have an int*. It is converted to a const void* and
printed.

> cout<<"*ptr is "<<*ptr<<endl;


*ptr is an int, and cout has a << operator to print ints.

> cout<<"The array is "<<array1<<endl;


As with array, array1 also decays to a pointer to its first element. But
this time, the type is char*, and that can convert to a const char* (==
char const*), for which cout has a special output operator: It is treated
as a null terminated string.

> cout<<"ptr1 is "<<ptr1<<endl;


Will call the same operator as array1 did.

> cout<<"*ptr1 is "<<*ptr1<<endl;


And this uses the char output operator.

> }
> 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".


Because the standard says const char* are to be treated specially by cout
and other ostream objects. In the same manner, wostream and wcout treat
const wchar_t* specially.

--
rbh
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-12-2007
* 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?


There's no difference in the behavior of the pointers or arrays, at the
language level.

There is a difference in how typical output functions deal with them.

A typical output function will assume that a pointer to char points to a
zero-terminated string.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing two strings with arrays and pointers agent349 C++ 5 03-10-2009 04:23 AM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Learning pointers and arrays of pointers With Files ketema@gmail.com C Programming 1 03-28-2005 03:51 AM
char arrays and integer arrays... why the difference? Bill Reyn C++ 3 06-22-2004 12:01 PM
Arrays and Pointers to Arrays kelvSYC C Programming 2 09-26-2003 06:52 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57