Thread: Pointer and array help!

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    7

    Pointer and array help!

    I am reading through a C book and I am up to 'pointer equivalency' and I am having trouble with this code snippet:
    Code:
    int i, array[10];
    int *pointer, *intPtr;
    
    pointer = array;
    i = 4; 
    
    //I know these two statements do the exact thing
    intPtr = &array[ i ];
    intPtr = pointer + i;
    
    //I need help with this stuff below
    int x;
    x = pointer[ i ];
    x = *(array+i)
    intPtr = &pointer[ i ];
    intPtr = array + i;
    The book then goes on to say that you can use pointers in these [] and that you can use array variable names as if they where pointers to the first element. I understand what the array variable name and how it points to the first element but I am having trouble understanding this snippet.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What exactly do you not understand about those lines?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    1
    x=pointer[i] when output will display the dereferenced value that the pointer "pointer" is pointing to. The pointer points to index four (4) of the array, e.g., array[4], which is uninitialized at this point - there may be garbage in there. Thus, if you set array[i]=some_integer, lets say, for this example, 4 ... then the output will be the integer 4.

    x=*(array+i) is an alternative to the above, and will result in the same output.

    intPtr = &pointer[i] has to be dereferenced when you go to output the intPtr value, since intPtr points to a memory location, e.g., the memory address of "pointer" at index i, which is 4. So intPtr points to the memory address of what is at array index 4. For instance, to see the value held in memory do the following:

    printf("this is the value stored in memory %d\n",*intPtr); //this should display the integer 4 as well.

    intPtr = array + i yields the same result the last example, above.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-28-2010, 02:44 AM
  2. Casting from a 1D array pointer to a scalar pointer
    By komkamol in forum C Programming
    Replies: 8
    Last Post: 09-25-2009, 01:44 AM
  3. pointer to pointer that points to a char array
    By steve1_rm in forum C Programming
    Replies: 2
    Last Post: 01-14-2009, 12:03 AM
  4. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  5. A pointer to a character pointer array... can't pass
    By Lynux-Penguin in forum C Programming
    Replies: 9
    Last Post: 10-12-2003, 10:53 PM