[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


Int Pointer vs Char Pointer

Posted on 2004-10-23
Medium Priority
Last Modified: 2008-02-01
More questions....

int a[4] = {3,6,5,9};
int* p = a;

cout<<a<<endl;   // outputs the address of a, same as &a
cout<<p<<endl;   // outputs the address of a
cout<<*p<<endl; //outputs 3, the first item in the array
cout<<a[0]<<endl; //outputs 3, the first item in the array

I understand that, so am i correct in assuming that a is a pointer by its self?

char b[4] = "hey";
char* c = b;

cout<<b<<endl;     //outputs hey ?
cout<<c<<endl;     //outputs hey
cout<<*c<<endl;   //outputs h
cout<<b[0]<<endl; //outputs h

I know this is a c-style string, so why is it that the pointer c to the string b is not set the the address of b, as in the int array? Where is this pointer pointing? I can get the address of b with &b, but why not c?

brain drain time. thanks.
Question by:bingie
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Assisted Solution

BenMorel earned 400 total points
ID: 12391283
>    I understand that, so am i correct in assuming that a is a pointer by its self?
Of course! A C "array" is always identified by the pointer to the first element
In this case, a is the pointer to the first element of the 4 int array

>    cout<<b<<endl;     //outputs hey ?
Yes, it outputs "hey".
This is a pointer to the first element of the array (here the first char of the string) so there is here no difference between b and c, both are char* and point to the same element.

If it doesn't display a memory address like the example before, it's because the types differ.
cout is an object that accepts a lot of data types.
At compilation time, your compiler will choose the most appropriated type to link to.
Here it is a char* , so it evaluates it as a C string, because the class which instanced cout was coded like this ;) It's a comprehensible programmer's choice ;)

You can use type-casting to cast your char* into int* for example, so that the compiler will not think you want to print the entire string, but only a memory address
Try this :
cout<< (int*) c <<endl;     //outputs 0xbbbbbbbb

See ya;)

Accepted Solution

GFinseth earned 400 total points
ID: 12391614
int b[3] = {1,2,3};           // b points to an array of int
char c[3] = "HI";             // c points to an array of chars
int * bp = b;                   // now bp points to the same data as b points to
char * cp = c;                // now cp points to the same data as c points to

Let's make up a memory map for this.

Addr  Variable  Type  Data
F000                int       1
F002                int       2
F004                int       3
F006                char    'H'
F008                char    'I'
F00A               char    '\0'
FF0C    bp       int *    F000 (points to start of int array)
FF0E    cp        char *  F006 (points to start of char array)

Did you notice that b and c are missing from the table?  They are not really variables.  They are constants.  The value of b is F000 (points to int array), and the
value of c is F006 (points to char array).

The expressions &b &c will give a warning in some compilers, because the & is not meaningful and does not change the value.  In effect, the & is ignored if you put it on the name of an array.

In the expressions &bp and &cp, on the other hard, the & does have meaning.  Because these are variables, as opposed to constants, they have to "live" somewhere in memory.  &bp is FF0C, and &cp is FF0E.

The name of an array behaves like a pointer to the beginning of the array, but an array is not a pointer VARIABLE.  It works for me to think of it as a pointer CONSTANT.  It's a pointer, but you can't change it.  Try these examples:

char a[]="Hello";
cout << a;
a = " bingie";             // this will not compile - error will say lvalue required - name of array cannot be assigned a new value
cout << a << endl;
char * a = "Hello";
cout << a;
a = "bingie";            // this is fine - a is a pointer variable and can be changed
cout << a << endl;

An "lvalue" is something that is valid on the left side of an assignment.  It's a "left-value", or in other words, a variable.

You also need to be careful about another difference.  Unless you initialize a pointer variable when it is declared, you have a pointer but have not allocated space for the data itself.  The pointer will not even point to valid writeable memory.  Here's something my students do all the time:

char * a;
cout << a;

You will probably get an exception error at run-time, since a is never initialized.  It points to who-knows-where, and then we write a string to that random location.  In this case, an array makes more sense.

In general, use arrays in parent functions and pointer variables in child functions.

                           Array               Variable
Example               char a[100]       char *
Behavior               constant            variable
Allocates for Data  yes                   no
Typical Use           parent function  child function

Hope this helps clarify things for you.

By the way, here's something I ask my students to figure out.

If a is an array or pointer...
a[i] is the same as *(a+i)
a[0] is the same as *a

The []'s are just a quick way to add an offset to a pointer and grab the data at that offset!


Expert Comment

ID: 12393124
Very nice explanation ;)
LVL 11

Author Comment

ID: 12393459
Great thanks!

Expert Comment

ID: 12394593
Thanks, BenMore.

Yours was also very nicely done.


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question