Thread: pointers and arrays

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    9

    pointers and arrays

    helloo alll,
    i am new to this C board. and i am very basic to this language. and i hope u all will help me out to know more about this. any way i have quick question about pointer,how do access the 2d array using the pointer arithmatic. and this is what my approch

    Code:
    for(i=0;i<2;i++)
       {
          for(j=0;j<3;j++)
          {
             printf("%d\n",*(a+i*3+j));
          }
       }
    outut
    Code:
    2293584
    2293596
    2293608
    2293620
    2293632
    2293644
    which give me the address instead of values

    is this way of accessing the elements
    Code:
    *(base address + row(i) * no.ofcol + col(j))
    but when i change this
    Code:
    printf("%d\n",*(*a+i*3+j));
    it gives me the proper output expecteed

    can any one explain me or if i am wrong correct me please

    thax

    -onthewaytoC

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int a[2][3] = { { 1, 2, 3}, {3, 6, 9 } };
    int i, j;
    for( i = 0; i < 2; ++i )
        for( j = 0; j < 3; ++j )
            printf("%d\n",*(*(a+i)+j));
    The identifier "a" can be looked at as a pointer-to-pointer-to-int (int **a). "a+i" is a pointer-to-pointer-to-int of row "i" of the 2d array which when dereferenced as in "*(a+i)" yields a pointer-to-int indicating the particular row. Adding "j" to this as in "*(a+i)+j" indicates a pointer-to-int to the particular column "j" of the particular row "i" we are looking at. Dereferencing that as in "*(*(a+i)+j)" yields the value at row "i" column "j".
    Last edited by hk_mp5kpdw; 06-08-2005 at 06:22 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    the c-dil
    Join Date
    May 2005
    Posts
    12
    Consider this

    Code:
     int arr[4][2]={
                              {1,2},
                              {3,4},
                              {5,6},
                              {7,8}
                          };
    Now , the compiler knows how many columns are there in the
    array arr ,since we specified this in the array declaration .
    So it interprets the expression arr[0] as (arr+0), and arr[1] as
    (arr+1).
    This way we are able to reach each individual row . Now
    suppose I want to refer to the element arr[2][1] using pointers.
    arr[2][1] can also be interpreted as (arr[2]+1) . value at this
    address can be obtained by using the value at address operator
    * i.e *(arr[2]+1) . arr[2][1] can be interpreted as *(*(arr+2)+1)

    hence all these expressions are same.

    arr[2][1]
    *(arr[2]+1)
    *(*(arr+2)+1)
    Rajat kochhar

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    9
    ooh thax hk_mp5kpdw for your reply.

    so if i have understood you , that the indentifier 'a' is pointer to pointer - an int right

    when i increment 'a' thats is
    Code:
    which when dereferenced as in "*(a+i)"  yields a pointer-to-int indicating the particular row
    so if i am incementing 'a' the offset is base address(i.e a) + i * 4bytes is it.

    if i am wrong please correct me

    thax u

    -onthewaytoC

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > The identifier "a" can be looked at as a pointer-to-pointer-to-int (int **a)
    Erm, no it isn't, and no it can't.

    > int a[2][3] = { { 1, 2, 3}, {3, 6, 9 } };
    When you say 'a', you get a pointer to the first element of a (the type being int (*)[3])
    This is NOT the same as int**
    http://www.eskimo.com/~scs/C-faq/q6.3.html

    So when you do (a+i), you're really getting a pointer to a[i] (with the aforementioned type). In memory terms, you now have a pointer to the start of an array containing say 3,6,9 (when i=1).

    Now when you do *(a+1), this is when you get an int* pointer.
    (*(a+1)+j), you have a pointer to a specific element of 3,6,9 (say a pointer to a memory location containing 9, when j=2 say).

    *(*(a+1)+j) dereferences that pointer to an int to get the actual integer (9 say).

    Code:
    #include <stdio.h>
    
    int main ( void ) {
      int a[2][3] = { { 1, 2, 3}, {3, 6, 9 } };
      int i, j;
      for( i = 0; i < 2; ++i )
          for( j = 0; j < 3; ++j ) {
            int (*p1)[3] = a;
            int *p2;
            int val;
            p1 += i;
            p2 = *p1 + j;
            val = *p2;
            printf("%p %p %d\n",(void*)p1,(void*)p2,val);
          }
      return 0;
    }
    Simply mushing p1 to be an int** for example will get you
    Code:
    $ gcc -Wall bar.c
    bar.c: In function `main':
    bar.c:8: warning: initialization from incompatible pointer type
    $ ./a.exe
    Segmentation fault (core dumped)
    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 2005
    Posts
    50
    to access arrays, you need to type array[which_one_it_is] = " whatever you want its value to be ";


    I am a very smart programmer, so don't mess with me
    DONT

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >to access arrays, you need to type array[which_one_it_is] = " whatever you want its value to be ";
    You don't need to, though that's by far the easiest way to get an an element in the array. However, your post really has no point since the question was concerning how to access arrays using pointer arithmetic, not the subscript operator.
    My best code is written with the delete key.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I am a very smart programmer
    Really.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    9
    thax very much guys i got it now

    -onthewaytoC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM