Thread: two dimensional array

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    21

    two dimensional array

    hello friends and thank you in advance .I have declared two dimensional array
    Code:
    int tab[2][2]={1,2,3,4};printf("%p",tab);printf("%p",*tab);
    the question:why tab and *tab give the same output result
    Code:
    0xbfc5523c0xbfc5523c

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    An array name used without the indices is, by default, a pointer to the first element of that array.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    thank' Matticus but tab is pointer to pointer (tab pointer to *tab) and *tab is pointer to first element of tab (pointer to tab[0][0]) that's true?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Question 6.18
    It's a very subtle point, but it's one you need to get to grips with.

    The address of each 'row' of your 2D array is a "pointer to an array", not a "pointer to a pointer".

    Examples
    Code:
    int main()
    {
      int arr[2][4];
      int *pa;
      int (*pb)[4];
    
      pa = &arr[0][0];
      pa = arr[0];
      pb = &arr[0];
      pb = arr;
    
      return 0;
    }
    Remember, arrays are contiguous blocks of memory, so whether you print the address of arr, arr[0] or arr[0][0], it's all just a pointer to a single location in memory. Yes, the type of each pointer is different, but the value is the same.

    Code:
    struct foo {
      int bar;
    };
    struct foo var;
    struct foo *p1 = &var;
    int *p2 = &var.bar;
    Again, since the first element of a struct is at the start of the struct, a pointer to the whole struct (p1) or a pointer to the first element (p2) will have different types, but the same value.
    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.

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    thank you Salem but i don't understand why tab and tab* give the same result (with static two dimensional array)but with dynamic two dimensional array the result is quite different tab it give the address of *tab

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    [code]#include
    #include
    main()
    {
    int i,j;
    int array[n][m];
    n=6;
    m=8;
    clrscr();
    printf(" Enter i and j : ");
    for(i=0;i

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by nadji View Post
    thank you Salem but i don't understand why tab and tab* give the same result (with static two dimensional array)but with dynamic two dimensional array the result is quite different tab it give the address of *tab
    Firstly, your example is not a static two dimensional array unless the definition of tab you showed is at file scope (outside a function, or prefixed with the static keyword inside a function).

    Second, I wrote a more detailed response, but cannot post it because the forum software reports an error, and demands extra code tags in several places where - if I put them in - would make the entire post virtually unreadable.

    This short summary, however, is that you need to understand that pointers and arrays are different things. The only way you would expect output for your (incorrectly named) "static two dimensional array" to be the same as output for a "dynamically allocated array" is if you believed a pointer and an array are exactly the same things. Which they ARE NOT.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3 dimensional array help!
    By kplax in forum C Programming
    Replies: 2
    Last Post: 10-18-2011, 03:36 PM
  2. Replies: 24
    Last Post: 11-11-2008, 01:39 PM
  3. 2 dimensional array
    By chasesg in forum C Programming
    Replies: 2
    Last Post: 10-13-2007, 12:12 PM
  4. Replies: 1
    Last Post: 04-25-2006, 12:14 AM
  5. Two-dimensional array
    By Nir in forum C Programming
    Replies: 3
    Last Post: 04-14-2003, 10:54 PM