2d arrays

This is a discussion on 2d arrays within the C Programming forums, part of the General Programming Boards category; I wrote this code. Code: int a[1][2]={{10,1}};int *b[1]={{10,1}}; int main(){ printf("%d",**a); //prints 10 printf("%d",**b); //gives error printf("%d",*b); //gives 10 } ...

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    3

    2d arrays

    I wrote this code.
    Code:
    int a[1][2]={{10,1}};int *b[1]={{10,1}};
    int main(){
        printf("%d",**a);   //prints 10
        printf("%d",**b);    //gives error
        printf("%d",*b);    //gives 10
    }
    Why does **b give error? And why does *b give 10? Shoudn't it give a pointer?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    > Why does **b give error?
    Because 10 isn't a valid memory address on your system.

    > And why does *b give 10?
    Because that's what you stored in the array to begin with.
    If you want to pretend that 10 is a pointer, then later on pretend that the pointer is an int, you get back to where you started.

    > Shoudn't it give a pointer?
    Perhaps you should use a decent compiler that will give you warnings about incompatible types in assignment.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    3
    I am a beginner in c and I'm not understanding what you said.


    I declared
    Code:
    int *b[]
    which is an array of pointers to int. So when I assign it
    Code:
    {{10,1}}
    , shouldn't *b give a pointer to where first element of the first array in b, ie 10, is stored, not 10 itself? Also, adding 1 to *b refers to first element of second array in b(not present here). How do I refer to the second element of first array in b, ie 1 ?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,203
    Nope. Incidentally, what you're doing is initialising, not assigning.

    The initialisers of an array of pointers initialise the pointers with the supplied values. The additional braces don't change that so
    Code:
        int *b[1]={{10,1}};
    is equivalent to
    Code:
        int *b[1]={10,1};
    which has the same net effect (including warnings from your compiler, if used with appropriate non-default warning levels) as
    Code:
        int *b[2];
        b[0] = 10;
        b[1] = 1;
    The type of shorthand you expect (initialisers that look like arrays leading to pointers being initialised to the first elements in those arrays) isn't what you get.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    3
    How could it have the same effect? b should have 1 element but you wrote 2.

    Let's say I wrote

    Code:
    int *b[]={{10,1},{5,3}};
    then b[0] should be 10, b[1] is 1 and so on. But b[1] is 5 and that's it.

    Also if I try

    Code:
    char *b[]={"ab","cde"};
    it works as I wrote before. Isn't "ab" just a character array?

    I am a bit confused.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    Perhaps you would be less confused if you stored integers in an integer array, and printed them as integers.

    Instead of all this hooky 'ints-as-pointers' rubbish which won't even compile on a modern compiler.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-11-2013, 10:57 PM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 04:31 AM
  4. Replies: 2
    Last Post: 02-23-2004, 05:34 AM
  5. separating line of arrays into array of arrays
    By robocop in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 12:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21