473,748 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2344

"ali" <tj@raha.com> wrote in message
news:94******** *************** *********@4ax.c om...
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.c om...
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<voi d*>(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******** ************@co mcast.com...

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

"ali" <tj@raha.com> wrote in message
news:94******** *************** *********@4ax.c om...
> 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
2498
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 *array, int newsize ) { if (array) delete array;
12
1854
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 will be appreciated!
10
4121
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
1539
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. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
7
2806
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 uiParam0; public uint uiParam1;
9
2197
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 simple array, to pass to a range constructor for a const vector. Here is some demonstration code: #include <iostream> using namespace std;
27
8968
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 advance. Erik
12
3882
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 looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
20
2170
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 suddenly I can't use sizeof(array) / sizeof(array) anymore within that function ? Help - What point am I missing ?
0
8832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9558
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9331
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9253
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8250
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6077
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.