Like Tree2Likes
  • 2 Post By anduril462

Incorrect pointer conversion when accessing a[2][2] with *b.

This is a discussion on Incorrect pointer conversion when accessing a[2][2] with *b. within the C Programming forums, part of the General Programming Boards category; Hi When running following code, it gives correct output but a warning something like: "Incorrect Pointer Conversion" Code: void main() ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    6

    Incorrect pointer conversion when accessing a[2][2] with *b.

    Hi

    When running following code, it gives correct output but a warning something like: "Incorrect Pointer Conversion"

    Code:
    void main()
    {
    	int a[2][2] = {{1,2}, {3,4}}, *b = &a;
    
    
    	printf("*b = %d", *b);
    	printf("*(b+1) = %d", *(b+1) );
    	printf("*(b+2) = %d", *(b+2) );
    	printf("*(b+3) = %d", *(b+3) );
    }

    please help me out with this, why is this incorrect? And how is a 2d pointer saved in memory? Isnt it in sequence like this - a[0][0], a[0][1], a[1][0], a[1][1]
    If this is correct, above method should work just fine ...

    Also please tell me all the methods one can use to access an array 2d, 3d anything, from a function ...

    Thanks in advance

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    #include <stdio.h>
    
    int main( void )
    {
        int a[2][2] = {{1,2}, {3,4}}, *b = &a[0][0];
     
        printf("*b = %d", *b);
        printf("*(b+1) = %d", *(b+1) );
        printf("*(b+2) = %d", *(b+2) );
        printf("*(b+3) = %d", *(b+3) );
        return 0;
    }
    Kurt

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > please help me out with this, why is this incorrect?
    Because (in your code), the type of &a is a pointer to an array - specifically int (*)[2][2]

    Code:
    $ cat foo.c
    #include <stdio.h>
    int main()
    {
        int a[2][2] = {{1,2}, {3,4}};
        
        // points to the whole array
        int (*b)[2][2] = &a;
    
        // points to a row of the array
        // x and &x[0] have the same type
        int (*c1)[2] = a;
        int (*c2)[2] = &a[0];
    
        // points to an element of the array
        // x and &x[0] have the same type
        int *d1 = a[0];
        int *d2 = &a[0][0];
    
        // lots of different pointers, with different types,
        // all pointing to the same place
        printf("%p %p\n%p %p\n%p %p\n",
               (void*)a, (void*)b,
               (void*)c1, (void*)c2,
               (void*)d1, (void*)d2 );
        return 0;
    }
    $ gcc foo.c
    $ ./a.out 
    0x7fff9f564fc0 0x7fff9f564fc0
    0x7fff9f564fc0 0x7fff9f564fc0
    0x7fff9f564fc0 0x7fff9f564fc0
    > And how is a 2d pointer saved in memory? Isnt it in sequence like this - a[0][0], a[0][1], a[1][0], a[1][1]
    Yes it is.

    But you need to be really careful about your terminology here.
    Arrays and Pointers
    Arrays and pointers are closely related, but you can't just mix and match using "pointer" and "array" in your descriptions without creating confusion.
    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.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,462
    Also, if you want help understanding more complex types/declarations in C, like what Salem posted, e.g.
    Code:
    int (*c1)[2]
    I find this site useful: Reading C type declarations
    laserlight and AndiPersti like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg fault when accessing double pointer
    By cerr in forum C Programming
    Replies: 14
    Last Post: 12-30-2009, 02:50 PM
  2. Replies: 9
    Last Post: 02-16-2009, 11:20 AM
  3. Accessing a pointer after it has been freed
    By dwks in forum C Programming
    Replies: 16
    Last Post: 10-18-2006, 06:48 AM
  4. HELP! Accessing pointer elements of a vector1
    By Oz_joker in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2005, 03:59 AM
  5. accessing structure pointer problem
    By godhand in forum C Programming
    Replies: 2
    Last Post: 04-09-2004, 10:52 PM

Tags for this Thread


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