Thread: Pointers

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    Pointers

    Hi,
    I am having trouble with creating array of pointers and accessing the elements in the array.

    For example:

    int a1[10][20];
    int a2[10][20];
    int a3[10][20];

    How do I create array of pointers to a1,a2,a3 that is equivalent to a[3][10][20]?

    Thanks for your help!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Why don't you just create a[3][10][20] ?

    Or if you really want to play with pointers...
    Code:
    int a1[10][20];
    int a2[10][20];
    int a3[10][20];
    
    int *ptr[3] = {a1,a2,a3};

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You might also take a look at: Arrays and Pointers


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    3

    reply

    CommonTater, thanks for your suggestion. Ii need to create an array of pointers because I have a set of "a" arrays that I need to use depends on the situation.

    If you use:
    Code:
    int *ptr[3] = {a1,a2,a3};
    How do I access the content of a1, a2 and a3 via ptr, such as a1[1][1]?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well CommonTater has the right idea, but you need to get the syntax right for "an array of pointers to 2D arrays".

    Something like this
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
      int   a1[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
      int   a2[2][3] = { { 10, 20, 30 }, { 40, 50, 60 } };
      int   (*a3[])[3] = { a1, a2 };
      int   x, y, z;
      for ( x = 0 ; x < 2 ; x++ ) {
        for ( y = 0 ; y < 2 ; y++ ) {
          for ( z = 0 ; z < 3 ; z++ ) {
            printf("%d, ",a3[x][y][z] );
          }
          printf("\n");
        }
      }
      return 0;
    }
    
    
    $ gcc bar.c
    $ ./a.out 
    1, 2, 3, 
    4, 5, 6, 
    10, 20, 30, 
    40, 50, 60,
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    3
    Salem,

    Thanks! That works great! Now, I am running into the next problem. How do i pass a3 into a function and how do I access it in the function?

    Thanks in advance for your help.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The copy/paste rule works.
    Simply copy the declaration of the array you want to pass, and make that the formal parameter.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    void foo ( int (*a3[])[3] ) {
      int   x, y, z;
      for ( x = 0 ; x < 2 ; x++ ) {
        for ( y = 0 ; y < 2 ; y++ ) {
          for ( z = 0 ; z < 3 ; z++ ) {
            printf("%d, ",a3[x][y][z] );
          }
          printf("\n");
        }
      }
    }
    
    int main()
    {
      int   a1[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
      int   a2[2][3] = { { 10, 20, 30 }, { 40, 50, 60 } };
      int   (*a3[])[3] = { a1, a2 };
      foo( a3 );
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  2. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  3. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  4. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM
  5. Pointers to Classes || pointers to structures
    By C++Child in forum C++ Programming
    Replies: 24
    Last Post: 07-30-2004, 06:14 PM