Thread: 2D-array pointer

  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 10:27 AM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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,336
    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,336
    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.)

  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
    278
    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
    278
    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,909
    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.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    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.

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, 01: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, 10: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, 10:53 PM