Help in pointers to array - C++

This is a discussion on Help in pointers to array - C++ within the C++ Programming forums, part of the General Programming Boards category; I've created an array and a pointer to it..i read in cprogramming.com that when you create a pointer to an ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    36

    Unhappy Help in pointers to array - C++

    I've created an array and a pointer to it..i read in cprogramming.com that when you create a pointer to an array you don't have to use the (&) sign and it gives the memory address of the first element to the pointer...
    what if i want memory address of other elements?

    please help..
    Last edited by tennisstar; 10-26-2012 at 08:52 AM.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by tennisstar View Post
    what if i want memory address of other elements?
    You use either (e.g. to get the address of element with index 2):

    Code:
    &a[2];
    a + 2;
    The above are both equivalent. As an aside, you should now see that you could have used "&a[0]" for the first element, and that this is equivalent to "a + 0", which can be used as just "a".
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    36
    Thanks a lot....
    it helped me very much and i made a successfully running program..
    here it is:
    Code:
    #include <iostream>
    using namespace std;
    
    
    int main(){
    
    
        int a[5];
        a[0] = 1;
        a[1] = 5;
        a[2] = 0;
        a[3] = 9;
        a[4] = 99;
        int *b = a;
    
    
        for(int a = 0; a < 5; a++){
    
    
            cout << "Array index " << a << "\tmemory address " << &b[a] << "\tvalue " << b[a] << "\n";
    
    
        }
        cin.get();
    
    
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,628
    Good. Note that instead of:
    Code:
    int a[5];
    a[0] = 1;
    a[1] = 5;
    a[2] = 0;
    a[3] = 9;
    a[4] = 99;
    you could have written:
    Code:
    int a[5] = {1, 5, 0, 9, 99};
    Also, as a matter of style, the double blank lines in your main function's body are unnecessary: a single blank line or in this case no blank line would do.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  2. Replies: 4
    Last Post: 07-01-2009, 01:53 PM
  3. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 08:21 AM
  4. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  5. Replies: 1
    Last Post: 10-21-2007, 07:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21