pointer and array help

A

ali

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
 
D

David Hilsee

ali said:
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.

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.
 
J

John Harrison

ali said:
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.

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.

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
 
M

Method Man

David Hilsee said:
ali said:
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.

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.

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.
 
J

John Harrison

David Hilsee said:
ali said:
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.

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.

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.

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.
You'd think they would have modified the code for C-style strings.

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
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Method said:
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top