Syntax for pointing to a array and acessing its elements

This is a discussion on Syntax for pointing to a array and acessing its elements within the C Programming forums, part of the General Programming Boards category; Hello, can somebody give me some help about using pointers to arrays? What I want to do is to use ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    30

    Syntax for pointing to a array and acessing its elements

    Hello, can somebody give me some help about using pointers to arrays?

    What I want to do is to use a array of integers. This is the come I came up with:

    int *myArray[10];

    myArray = (void*)malloc(sizeof(int) *10);

    Is that correct? How can I access the 2nd element of my array? I tried *myArray[2] but it didn't work.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    These are different:
    Code:
    int *myArray[10];
    int myArray[10];
    The first is an array of pointers to integers. The second is an array of integers.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    30
    Ah, thank you! Just to make sure I'm making sense of pointers, would this be correct:

    Code:
    int *MyArray[10];
    
    for (i = 0; i <= 10; i++)
    {
        myArray[0] = (int*)malloc(sizeof(int) );
    }
    and to access elements:


    Code:
     *myArray[0] = 1;
     *myArray[1] = 1;

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Do you want a pointer to point at an array of integers?
    If so you can do like this:

    Code:
    int main()
    {
            int array[3] = {1,2,3};
            int *ptr;
    
            ptr = array;
    
            printf("%d\n", *ptr);
            printf("%d\n", *ptr+1);
    
            //or perhaps better, like this
            printf("%d\n", ptr[0]);
            printf("%d\n", ptr[1]);
    
    
            return 0;
    }
    Last edited by Subsonics; 11-03-2009 at 11:16 AM.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by papagaio View Post
    Ah, thank you! Just to make sure I'm making sense of pointers, would this be correct:

    Code:
    int *MyArray[10];
    
    for (i = 0; i <= 10; i++)
    {
        myArray[0] = (int*)malloc(sizeof(int) );
    }
    and to access elements:


    Code:
     *myArray[0] = 1;
     *myArray[1] = 1;

    Close.
    Code:
    int *MyArray[10];
    
    for (i = 0; i <= 10; i++)
    {
        myArray[i] = malloc(sizeof(int) );
    }
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    24,009
    Close. But still not right.
    Code:
    int *MyArray[10];
    
    for (i = 0; i <= 9; i++)
    {
        myArray[i] = malloc(sizeof(int));
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    or more commonly:
    Code:
    int *MyArray[10];
    
    for (i = 0; i < 10; i++)
    {
        myArray[i] = malloc(sizeof(*myArray[i]));
    }
    and the 10 may well be replaced by a named constant.
    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

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