Int Pointer vs Char Pointer

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.
LVL 11
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

GFinsethConnect With a Mentor Commented:
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!

BenMorelConnect With a Mentor Commented:
>    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;)
Very nice explanation ;)
bingieAuthor Commented:
Great thanks!
Thanks, BenMore.

Yours was also very nicely done.

All Courses

From novice to tech pro — start learning today.