Results 1 to 4 of 4

Thread: pointers in C

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    425

    Unhappy pointers in C

    how do i point to a 2 dimensional array in C?

    int array[3][3] = { {1,2,3} ,
    {3,2,1},
    {1,2,3}};

    int *ptArray;

    ptArray = array;

    if pointing to a 1 dimensional array i would use *(ptArray+2) to point to the 3rd element of a 1D array.

    how can i point to a 2D array lets say array[3][1] from the above example?

    Thanks

    i am just starting to learn C

  2. #2
    New Member
    Join Date
    Apr 2002
    Posts
    5
    You can't actually use a pointer to point to a 2-D array. You have to use a pointer to a pointer ( **) That means that you use one pointer(*) to point to the row of the 2-D array and use another pointer(**) to point to the column of the 2-D array.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    425
    can you show me an example of 2 pointers pointing to a 2D array?
    is this the so called "double pointers"?

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It's better to use a single pointer to point to an array and use this simple formula to calculate the 1d offset:
    (an array with n dimensions labeled a, b, c...)
    offset = a + size(a)*b + size(b)*c + ...
    where size(x) means: the offsets this dimension occupies, in case of an array
    ar[5][6][7];
    it is
    size(a) = 5
    size(b) = 30 (5*6)
    size(c) = 210 (5*6*7)

    Pointers to pointers soon get confusing!
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width