473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

pointer and array help

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
Jul 22 '05 #1
5 2301

"ali" <tj@raha.com> wrote in message
news:94********************************@4ax.com...
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.

--
David Hilsee
Jul 22 '05 #2
On Tue, 3 Aug 2004 21:20:56 -0400, David Hilsee
<da*************@yahoo.com> wrote:

"ali" <tj@raha.com> wrote in message
news:94********************************@4ax.com...
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
Jul 22 '05 #3

"David Hilsee" <da*************@yahoo.com> wrote in message
news:DM********************@comcast.com...

"ali" <tj@raha.com> wrote in message
news:94********************************@4ax.com...
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.

--
David Hilsee


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.
Jul 22 '05 #4
On Wed, 4 Aug 2004 02:07:41 -0400, Method Man <a@b.c> wrote:

"David Hilsee" <da*************@yahoo.com> wrote in message
news:DM********************@comcast.com...

"ali" <tj@raha.com> wrote in message
news:94********************************@4ax.com...
> 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.

--
David Hilsee


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
Jul 22 '05 #5
Method Man wrote:
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.

--
Salu2
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: xuatla | last post by:
I encountered "segmentation fault" and I checked my code, found the following problem: I want to reallocate memory for an array. I defined the following function: int reallocateMemory( double...
12
by: Davy | last post by:
I found a interesting pointer problem. What does the pointer mean? EXTERN short (*blocks); And what does the malloc mean? blocks = (short (*))malloc(count*sizeof(short )); Any suggestions...
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
2
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
7
by: Kathy Tran | last post by:
Hi, Could you please help me how to declare an araay of pointer in C#. In my program I declared an structure public struct SEventQ { public uint uiUserData; public uint uiEvent; public uint...
9
by: shaun | last post by:
Dear all, I realized an error in a previous post, I reproduce it here because I'm still not sure how to solve it: I want to make a templated function which points to one-past-the-end of a...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
20
by: jason | last post by:
Hello, I'm a beginning C programmer and I have a question regarding arrays and finding the number of entries present within an array. If I pass an array of structures to a function, then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.