Connecting Tech Pros Worldwide Help | Site Map

pointer and array help

ali
Guest
 
Posts: n/a
#1: Jul 22 '05
Hi,

I'm trying to understand the reason for different output on the
following codes

Code1:

#include <iostream.h>

int main()
{
int array[]={2,4,5};

int *pointer =0;

pointer=array;

cout<<pointer<<endl;

return 0;
}


Code2:

#include <iostream.h>

int main()
{
char array[]="words";

char *pointer =0;

pointer=array;

cout<<pointer<<endl;

return 0;
}


Code1 gives me the output as the hexadecimal value of the first
element of the array, which is what i expected the pointer to contain
(memory address), but code2 gives me the output 'words', instead of
the hexadecimal value of the first element of the arrray.

Am i missing something out in understanding the concept?

Will appreciate some help on explaining that.

Thanks,

TJ
David Hilsee
Guest
 
Posts: n/a
#2: Jul 22 '05

re: pointer and array help



"ali" <tj@raha.com> wrote in message
news:94d0h0tacu9hr3soc4jnqah6bvj4ko2io0@4ax.com...[color=blue]
> Hi,
>
> I'm trying to understand the reason for different output on the
> following codes
>
> Code1:
>
> #include <iostream.h>
>
> int main()
> {
> int array[]={2,4,5};
>
> int *pointer =0;
>
> pointer=array;
>
> cout<<pointer<<endl;
>
> return 0;
> }
>
>
> Code2:
>
> #include <iostream.h>
>
> int main()
> {
> char array[]="words";
>
> char *pointer =0;
>
> pointer=array;
>
> cout<<pointer<<endl;
>
> return 0;
> }
>
>
> Code1 gives me the output as the hexadecimal value of the first
> element of the array, which is what i expected the pointer to contain
> (memory address), but code2 gives me the output 'words', instead of
> the hexadecimal value of the first element of the arrray.
>
> Am i missing something out in understanding the concept?
>
> Will appreciate some help on explaining that.[/color]

In C (and also in C++, because it came from C), character arrays with a null
byte at the end can be used as strings. In C++, we have other standard
options for manipulating characters, but in C, the plain old null-terminated
array is it. Thanks to the existence of the C-style string, a lot of code
that takes a char */const char */etc assumes that the pointer is a pointer
to the first element of a null-terminated character array that should be
treated as a string. You have just stumbled upon one of those methods. The
array that you created is a null-terminated array of chars of size 6 (+1 for
the terminator), and you passed a pointer to the first element to a method
that takes a pointer to an array of characters and assumes that you wish to
treat them as a C-style string. Therefore, your code does not display the
address and instead displays the contents of the array.

--
David Hilsee


John Harrison
Guest
 
Posts: n/a
#3: Jul 22 '05

re: pointer and array help


On Tue, 3 Aug 2004 21:20:56 -0400, David Hilsee
<davidhilseenews@yahoo.com> wrote:
[color=blue]
>
> "ali" <tj@raha.com> wrote in message
> news:94d0h0tacu9hr3soc4jnqah6bvj4ko2io0@4ax.com...[color=green]
>> Hi,
>>
>> I'm trying to understand the reason for different output on the
>> following codes
>>
>> Code1:
>>
>> #include <iostream.h>
>>
>> int main()
>> {
>> int array[]={2,4,5};
>>
>> int *pointer =0;
>>
>> pointer=array;
>>
>> cout<<pointer<<endl;
>>
>> return 0;
>> }
>>
>>
>> Code2:
>>
>> #include <iostream.h>
>>
>> int main()
>> {
>> char array[]="words";
>>
>> char *pointer =0;
>>
>> pointer=array;
>>
>> cout<<pointer<<endl;
>>
>> return 0;
>> }
>>
>>
>> Code1 gives me the output as the hexadecimal value of the first
>> element of the array, which is what i expected the pointer to contain
>> (memory address), but code2 gives me the output 'words', instead of
>> the hexadecimal value of the first element of the arrray.
>>
>> Am i missing something out in understanding the concept?
>>
>> Will appreciate some help on explaining that.[/color]
>
> In C (and also in C++, because it came from C), character arrays with a
> null
> byte at the end can be used as strings. In C++, we have other standard
> options for manipulating characters, but in C, the plain old
> null-terminated
> array is it. Thanks to the existence of the C-style string, a lot of
> code
> that takes a char */const char */etc assumes that the pointer is a
> pointer
> to the first element of a null-terminated character array that should be
> treated as a string. You have just stumbled upon one of those methods.
> The
> array that you created is a null-terminated array of chars of size 6 (+1
> for
> the terminator), and you passed a pointer to the first element to a
> method
> that takes a pointer to an array of characters and assumes that you wish
> to
> treat them as a C-style string. Therefore, your code does not display
> the
> address and instead displays the contents of the array.
>[/color]

And should you wish to display the address instead of the content of the
string, change your code like this

cout << static_cast<void*>(pointer) << endl;

By casting your pointer from char* to void* you ensure that it is treated
as a memory address not as a pointer to a string.

john
Method Man
Guest
 
Posts: n/a
#4: Jul 22 '05

re: pointer and array help



"David Hilsee" <davidhilseenews@yahoo.com> wrote in message
news:DMidncSCLZ15pI3cRVn-qQ@comcast.com...[color=blue]
>
> "ali" <tj@raha.com> wrote in message
> news:94d0h0tacu9hr3soc4jnqah6bvj4ko2io0@4ax.com...[color=green]
> > Hi,
> >
> > I'm trying to understand the reason for different output on the
> > following codes
> >
> > Code1:
> >
> > #include <iostream.h>
> >
> > int main()
> > {
> > int array[]={2,4,5};
> >
> > int *pointer =0;
> >
> > pointer=array;
> >
> > cout<<pointer<<endl;
> >
> > return 0;
> > }
> >
> >
> > Code2:
> >
> > #include <iostream.h>
> >
> > int main()
> > {
> > char array[]="words";
> >
> > char *pointer =0;
> >
> > pointer=array;
> >
> > cout<<pointer<<endl;
> >
> > return 0;
> > }
> >
> >
> > Code1 gives me the output as the hexadecimal value of the first
> > element of the array, which is what i expected the pointer to contain
> > (memory address), but code2 gives me the output 'words', instead of
> > the hexadecimal value of the first element of the arrray.
> >
> > Am i missing something out in understanding the concept?
> >
> > Will appreciate some help on explaining that.[/color]
>
> In C (and also in C++, because it came from C), character arrays with a[/color]
null[color=blue]
> byte at the end can be used as strings. In C++, we have other standard
> options for manipulating characters, but in C, the plain old[/color]
null-terminated[color=blue]
> array is it. Thanks to the existence of the C-style string, a lot of code
> that takes a char */const char */etc assumes that the pointer is a pointer
> to the first element of a null-terminated character array that should be
> treated as a string. You have just stumbled upon one of those methods.[/color]
The[color=blue]
> array that you created is a null-terminated array of chars of size 6 (+1[/color]
for[color=blue]
> the terminator), and you passed a pointer to the first element to a method
> that takes a pointer to an array of characters and assumes that you wish[/color]
to[color=blue]
> treat them as a C-style string. Therefore, your code does not display the
> address and instead displays the contents of the array.
>
> --
> David Hilsee
>
>[/color]

Man, that's confusing for n00bs. I mean, if he really wanted to print the
contents of the string, a simple pointer dereference would suffice.

You'd think they would have modified the code for C-style strings.


John Harrison
Guest
 
Posts: n/a
#5: Jul 22 '05

re: pointer and array help


On Wed, 4 Aug 2004 02:07:41 -0400, Method Man <a@b.c> wrote:
[color=blue]
>
> "David Hilsee" <davidhilseenews@yahoo.com> wrote in message
> news:DMidncSCLZ15pI3cRVn-qQ@comcast.com...[color=green]
>>
>> "ali" <tj@raha.com> wrote in message
>> news:94d0h0tacu9hr3soc4jnqah6bvj4ko2io0@4ax.com...[color=darkred]
>> > Hi,
>> >
>> > I'm trying to understand the reason for different output on the
>> > following codes
>> >
>> > Code1:
>> >
>> > #include <iostream.h>
>> >
>> > int main()
>> > {
>> > int array[]={2,4,5};
>> >
>> > int *pointer =0;
>> >
>> > pointer=array;
>> >
>> > cout<<pointer<<endl;
>> >
>> > return 0;
>> > }
>> >
>> >
>> > Code2:
>> >
>> > #include <iostream.h>
>> >
>> > int main()
>> > {
>> > char array[]="words";
>> >
>> > char *pointer =0;
>> >
>> > pointer=array;
>> >
>> > cout<<pointer<<endl;
>> >
>> > return 0;
>> > }
>> >
>> >
>> > Code1 gives me the output as the hexadecimal value of the first
>> > element of the array, which is what i expected the pointer to contain
>> > (memory address), but code2 gives me the output 'words', instead of
>> > the hexadecimal value of the first element of the arrray.
>> >
>> > Am i missing something out in understanding the concept?
>> >
>> > Will appreciate some help on explaining that.[/color]
>>
>> In C (and also in C++, because it came from C), character arrays with a[/color]
> null[color=green]
>> byte at the end can be used as strings. In C++, we have other standard
>> options for manipulating characters, but in C, the plain old[/color]
> null-terminated[color=green]
>> array is it. Thanks to the existence of the C-style string, a lot of
>> code
>> that takes a char */const char */etc assumes that the pointer is a
>> pointer
>> to the first element of a null-terminated character array that should be
>> treated as a string. You have just stumbled upon one of those methods.[/color]
> The[color=green]
>> array that you created is a null-terminated array of chars of size 6 (+1[/color]
> for[color=green]
>> the terminator), and you passed a pointer to the first element to a
>> method
>> that takes a pointer to an array of characters and assumes that you wish[/color]
> to[color=green]
>> treat them as a C-style string. Therefore, your code does not display
>> the
>> address and instead displays the contents of the array.
>>
>> --
>> David Hilsee
>>
>>[/color]
>
> Man, that's confusing for n00bs. I mean, if he really wanted to print the
> contents of the string, a simple pointer dereference would suffice.
>[/color]

It is confusing but a pionter dereference would have printed the character
that the pointer was pointing at, not the whole string. That behaviour is
at least consistent, irrespective of the type of the pointer.
[color=blue]
> You'd think they would have modified the code for C-style strings.
>[/color]

What would you expect the following to print?

cout << "hello world\n";

"hello world\n", is a character array just like ali's example, and
obviously you wouldn't want the address of that array printed you want the
string printed.

john
Julián Albo
Guest
 
Posts: n/a
#6: Jul 22 '05

re: pointer and array help


Method Man wrote:
[color=blue]
> Man, that's confusing for n00bs. I mean, if he really wanted to print the
> contents of the string, a simple pointer dereference would suffice.[/color]

If you dereference a pointer to char you obtain a char, not a string.

And yes, can be confusing, many things in C++ can be. Simply because not be
confusing to newbies is not between the design principles of C++. It will
be impossible to do that and be highly compatible with C at the same time.

--
Salu2
Closed Thread