Thread: Pointers and 2-D arrays

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    43

    Pointers and 2-D arrays

    Consider the following piece of code :

    Code:
    int s[4][2] = {
                       {1, 2},
                       {3, 4},
                       {5, 6},
                       {7, 8}
                      };
    
    int * p;
    int (*pp)[2];
    
     for(int i = 0; i < 4; i++)
     {
       p = &s[i]; //incorrect 
       p = (int *)&s[i]; //alternative 1
       pp = &s[i]; //alternative 2
     }
    I am aware of the fact that in C, multidimensional arrays are stored as array of arrays & so on. If the 2-D array is split into 4 single dimensional arrays (as in the above example) and &s[i] returns the address of each of these single dimensional arrays, why can't it be simply assigned to an integer pointer ? Also, exactly what is the implication of this syntax - int (*pp)[2] ?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Hi Trish...

    Have you tried ...
    Code:
    int *ptr;
    
    int array[4][10];
    
    // initialize array...
    
    ptr = &array[3][4];
    that will isolate a single element from the array.

    If you want to point to an entire row... p = s[i]; should work...

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    43
    p = s[i] will work just fine & I am also aware of the syntax needed to access a particular element as pointed out. My question though, is about the statements commented out as alternatives 1 & 2 and the dreaded int (*pp)[2]...

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    All int *pp[2] is doing is creating an array of two int pointers:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void){
    
    	int *pp[2];
    	int x, y;
    
    	x = 5;
    	y = 4;
    
    	pp[0] = &x;
    	pp[1] = &y;
    
    	x = 3;
    	y = 6;
    
    	printf("%d %d", *pp[0], *pp[1]);
    
    	getchar();
    	return (0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by trish
    Also, exactly what is the implication of this syntax - int (*pp)[2] ?
    It declares pp to be a pointer to an array of 2 ints. Contrast this with an array of two pointers to int, as shown in AndrewHunter's example.
    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

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Alright, with the example you posted:
    Code:
    ...
    int (*pp)[2];
    ..
    pp = &s[i]; //alternative 2
    During your loop s[0] will point to a single array carrying 2 ints. So it would be equivalent to write:
    Code:
    int myArray[2] = {1,2};
    Now with int (*pp)[2] you can think of it as creating a pointer to an int[2] array. So to assign that pointer you would write:
    Code:
    int myArray[2] = {1,2};
    int (*pp)[2] = &myArray;
    
    printf("%d", (*pp)[0]); //will print 1
    Last edited by AndrewHunter; 07-06-2011 at 10:10 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    43
    Quote Originally Posted by laserlight View Post
    It declares pp to be a pointer to an array of 2 ints. Contrast this with an array of two pointers to int, as shown in AndrewHunter's example.
    Now with int (*pp)[2] you can think of it as creating a pointer to an int[2] array.
    How exactly is a normal pointer any different from a pointer to an array ? Any array, even a multidimensional one, even when its sub arrays are extracted, would yield a base address...why can't a normal pointer point to it ? Additionally, going by the jargon, 'pointer to an array', why can't it point to a single dimensional array, something like this :

    Code:
    int a[2] = {0, 1};
    int (*p)[2];
    
    p = &a[0];

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by trish
    How exactly is a normal pointer any different from a pointer to an array ?
    A pointer to an array is a "normal pointer"

    Quote Originally Posted by trish
    Any array, even a multidimensional one, even when its sub arrays are extracted, would yield a base address...why can't a normal pointer point to it ?
    What is a "normal pointer" to you?

    Quote Originally Posted by trish
    Additionally, going by the jargon, 'pointer to an array', why can't it point to a single dimensional array, something like this :
    Let's see... a is an array of 2 ints. Therefore, a[0] is an int. So, you are trying to make p point to an int, not to an array.
    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

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Here is a link to a good tutorial on pointers It may help answer your questions.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    43
    Quote Originally Posted by laserlight View Post
    A pointer to an array is a "normal pointer"


    What is a "normal pointer" to you?
    I meant the human readable int * p as a normal pointer & the vague looking (int * p)[2] as the weird one

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  2. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  3. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 01:48 PM
  4. pointers to arrays
    By gaah in forum C++ Programming
    Replies: 16
    Last Post: 03-08-2005, 05:59 PM
  5. 2D Arrays and pointers
    By kwigibo in forum C Programming
    Replies: 4
    Last Post: 02-20-2002, 01:16 PM