arrays in c programming

Discussion in 'Programming' started by vikas_r_s, Jun 22, 2006.

Thread Status:
Not open for further replies.
  1. vikas_r_s

    vikas_r_s New Member

    Joined:
    Jan 17, 2005
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    hi everyone,
    i am a newbi to programming.
    can any one tell me in c language a two dimensional array name is of what type?
    how come in arr[3][3], arr and *arr give same value(address)?
     
  2. plsoft

    plsoft New Member

    Joined:
    Mar 14, 2005
    Messages:
    234
    Likes Received:
    1
    Trophy Points:
    0
    when we use the '*' we are referring to the value of that variable (array in this case) which the pointer is pointing to, not the value of the pointer itself. However, if you use the 'address of' (&) operator, it will give the memory address of the variable it points to.
     
  3. tuxfan

    tuxfan New Member

    Joined:
    Feb 4, 2004
    Messages:
    2,653
    Likes Received:
    9
    Trophy Points:
    0
    Location:
    Mumbai
    Study the pointers well and you will understand this. Go for Pointers in C by Yashwant Kanetkar. Its a small book, worth its price. He will explain you pointers better than any of us. :) Pointers are pretty powerful (and desctructive if misused)!
     
  4. OP
    OP
    vikas_r_s

    vikas_r_s New Member

    Joined:
    Jan 17, 2005
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    plsoft waht you say is true in a uni dimensional array.
    but try the following code
    int main()
    {
    int a[3][3];
    printf("%d\n",a);
    printf("%d\n",*a);
    system("PAUSE");
    return 0;
    }

    both printfs give same value i.e. memory address.HOW?
     
  5. bukaida

    bukaida New Member

    Joined:
    Jul 21, 2005
    Messages:
    326
    Likes Received:
    1
    Trophy Points:
    0
    Location:
    KOLKATA
    The name of the array is the pointer to the first element of that array. Like wise a[5] and 5[a] will give you the same value. Go through the Kanitkar book(study the array and pointer chapter).
     
    Last edited: Jun 22, 2006
  6. casanova

    casanova The Frozen Nova

    Joined:
    Sep 25, 2004
    Messages:
    1,641
    Likes Received:
    2
    Trophy Points:
    0
    Location:
    Trespasser in Virtual Land
    Pointers is a very vast concept, there are dedicated books for it and yashwant kanetkar says "The no. of times your comp will hang is directly proportional to the no. of pointers you use".
    BTW * mans value at
    and & means address of.
    So by using * u get the value.
     
  7. ilugd

    ilugd Beware of the innocent

    Joined:
    Dec 25, 2005
    Messages:
    1,009
    Likes Received:
    3
    Trophy Points:
    0
    your array has 9 elements like if the first element is at memory loc 0 then the elements are there upto address 8.
    a is from 0 to 8
    a[0] is elements at 0,1,2
    a[1] is elements at 3,4,5
    a[2] is elements at 6,7,8

    value of a is 0 and *a that is a[0] is 0 again. That is how i understand it. But my c is a bit rusty around pointers. Am I right anyone?
     
  8. hitesh_hg

    hitesh_hg New Member

    Joined:
    Oct 28, 2003
    Messages:
    138
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    NCR
    Sorry but i couldn't bear to read all the above responses and dunno if it has already been answered but here is how it is..

    Since arr[3][3] is a 2D array you are basically creating a 2nd level pointer.
    hence arr & *arr point to same address a[0][0]. to refer to the value use **arr .

    And yes over use of pointer is lethal (for the brain:D)
     
Thread Status:
Not open for further replies.

Share This Page