Like Tree2Likes

2D-array pointer

This is a discussion on 2D-array pointer within the C Programming forums, part of the General Programming Boards category; Hello guys, I'm now studying the pointers. There's a code I found in a book that worked on Netbeans ( ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    27

    2D-array pointer

    Hello guys,

    I'm now studying the pointers. There's a code I found in a book that worked on Netbeans ( using GCC compiler ) and didn't work on Microsoft VS 2010 express.

    Code:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int s[5][2] = {
    		{ 1234, 56 },
    		{ 1212, 33 },
    		{ 1434, 80 },
    		{ 1312, 78 }
    	} ;
    	int ( *p )[2] ;
    	int i, j, *pint ;
    	for ( i = 0 ; i <= 3 ; i++ )
    	{
    		p = &s[i] ;
    		pint = p ;
    		printf ( "\n" ) ;
    		for ( j = 0 ; j <= 1 ; j++ )
    			printf ( "%d ", *( pint + j ) ) ;
    	}
    	return 0;
    }
    Why?

    The error message ( MS VS ):
    Code:
    1>some_destination\main.cpp(16): error C2440: '=' : cannot convert from 'int (*)[2]' to 'int *'
    Thanks in advance.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    int ( *p )[2] ;
    Should be

    Code:
    int *p[2];
    There are other errors as well... for example: s is a 2 dimentional array which you are using as a single linear array.

    On MSVS you are compiling the program as C++ and running smack into some of the incompatibilities between standard C and C++ ...

    Fix the errors and recompile on MSVS as a C program... (set in your build options panels)
    Last edited by CommonTater; 05-20-2011 at 11:27 AM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    C++ or C? C will let you convert pointers around, C++ will not.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by CommonTater View Post
    Code:
    int ( *p )[2] ;
    Should be

    Code:
    int *p[2];
    These aren't the same thing, and the rest of the code assumes the first form, not the second.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    These aren't the same thing, and the rest of the code assumes the first form, not the second.
    Ok, not having seen the first form before, except as pointer-to-function... what's the difference?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    int ( *p )[2] ;
    means that p is a pointer to int[2] (i.e. two consecutive ints). Only one pointer's worth of memory is allocated. (That is, a pointer is declared, no array is created.)

    Code:
    int *p[2];
    means that p is an array of two int*. Two pointers' worth of memory is allocated. (That is, an array is created.)
    CommonTater likes this.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    Quote Originally Posted by tabstop View Post
    Code:
    int ( *p )[2] ;
    means that p is a pointer to int[2] (i.e. two consecutive ints). Only one pointer's worth of memory is allocated. (That is, a pointer is declared, no array is created.)

    Code:
    int *p[2];
    means that p is an array of two int*. Two pointers' worth of memory is allocated. (That is, an array is created.)
    so in the first one, no space is reserved except for the pointer and in the second the space is reversed. Did I get it right?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    Code:
    int ( *p )[2] ;
    means that p is a pointer to int[2] (i.e. two consecutive ints). Only one pointer's worth of memory is allocated. (That is, a pointer is declared, no array is created.)

    Code:
    int *p[2];
    means that p is an array of two int*. Two pointers' worth of memory is allocated. (That is, an array is created.)
    How about that... you learn something new every day!

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    IIRC, some confusion can be avoided by instead of

    Code:
    int *p;
    you get in the habit of writing:
    Code:
    int* p;
    so that when you're presented with
    Code:
    int *p[2];
    you realize it's really
    Code:
    int* p[2];
    One must wrap the *p with parens to get something different
    Code:
    int ( *p )[2];
    Have I got that right?

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    How about that... you learn something new every day!
    Is that two days in a row?

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mike65535 View Post
    Is that two days in a row?
    Way more than that...

    I was pretty confident in C when I came here... but have easily doubled my insight since.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Quote Originally Posted by mike65535 View Post
    IIRC, some confusion can be avoided by instead of

    Code:
    int *p;
    you get in the habit of writing:
    Code:
    int* p;
    so that when you're presented with
    The latter (int* p) is often advocated against for one big reason. It gives the impression that the pointerness is part of the type of all variables on that line, when it only applies to the one variable. E.g.
    Code:
    int* p1, p2, p3;  // this seems to imply that p1, p2 and p3 are all pointers to ints, but really, only p1 is a pointer, p2 and p3 are regular ints
    int *p1, p2, p3;  // this associates the pointerness with p1 instead of the whole line
    Sure, it cuts both ways, as you have suggested, but most literature I've read prefers putting the * near the variable name instead of the type. I guess because a pointer to a single item is more common than a pointer to an array.
    mike65535 likes this.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by anduril462 View Post
    The latter (int* p) is often advocated against for one big reason. It gives the impression that the pointerness is part of the type of all variables on that line, when it only applies to the one variable.
    At this point, someone is legally obligated to mention that if you put each variable declaration on its own line, then no problem.

    (No I don't put one variable on each line either.)

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    Quote Originally Posted by anduril462
    The latter (int* p) is often advocated against for one big reason. It gives the impression that the pointerness is part of the type of all variables on that line, when it only applies to the one variable.
    Because of this I advocate against declaring multiple variables in a single statement (at least where pointers are involved).

    Quote Originally Posted by anduril462
    most literature I've read prefers putting the * near the variable name instead of the type.
    My style varies based on whether I'm programming in C or C++. See Stroustrup's answer to the FAQ: Is ``int* p;'' right or is ``int *p;'' right?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Code:
    int* p1, p2, p3;  // this seems to imply that p1, p2 and p3 are all pointers to ints, but really, only p1 is a pointer, p2 and p3 are regular ints
    This is the kind of "trick" (or rather, gotcha) worthy as a "puzzler" or test question. Thanks!

    I, too, avoid putting multiple declarations on a single line unless they are simple. No pointers, arrays, structs... I even avoid initialized variables on one line.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-28-2010, 02:44 AM
  2. Casting from a 1D array pointer to a scalar pointer
    By komkamol in forum C Programming
    Replies: 8
    Last Post: 09-25-2009, 02:44 AM
  3. pointer to pointer that points to a char array
    By steve1_rm in forum C Programming
    Replies: 2
    Last Post: 01-14-2009, 12:03 AM
  4. Replies: 1
    Last Post: 03-24-2008, 11:16 AM
  5. A pointer to a character pointer array... can't pass
    By Lynux-Penguin in forum C Programming
    Replies: 9
    Last Post: 10-12-2003, 11:53 PM

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