Solved

Int Pointer vs Char Pointer

Posted on 2004-10-23
4,104 Views
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.
0
Question by:bingie
    5 Comments
     
    LVL 7

    Assisted Solution

    by:BenMorel
    >    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;)
    Ben
    0
     
    LVL 1

    Accepted Solution

    by:
    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;
    strcpy(a,"abcdefg");
    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!

    0
     
    LVL 7

    Expert Comment

    by:BenMorel
    Very nice explanation ;)
    0
     
    LVL 11

    Author Comment

    by:bingie
    Great thanks!
    0
     
    LVL 1

    Expert Comment

    by:GFinseth
    Thanks, BenMore.

    Yours was also very nicely done.

    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    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.

    856 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now