Help printing a multi array

This is a discussion on Help printing a multi array within the C Programming forums, part of the General Programming Boards category; I am having difficulty trying to use a pointer to print contents of an array. With the simple array below ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Help printing a multi array

    I am having difficulty trying to use a pointer to print contents of an array. With the simple array below how could I use a pointer to print out the contents? I know I can use multi[x][x] but I would like to use a pointer instead. Is this where I need to learn casting? I am just printing different variables so I can learn how this works. Any help using a pointer of some type to print the contents of my array is greatly appreciated. One row at a time would be nice.

    Code:
       int multi[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
        
       int (*ptr)[4], count;
       
       ptr = multi;
      
    
       printf("%d\n", ptr);
       printf("%d\n", *ptr);
       printf("%d\n", multi);
       printf("%d\n", multi[0]);
       printf("%d\n", multi[0][0]);
    
        ++ptr;
     
       printf("%d\n", ptr);
       printf("%d\n", *ptr);
       printf("%d\n", multi);
       printf("%d\n", multi[0]);
       printf("%d\n", multi[0][0]);
       printf("%u\n", sizeof(multi[0]));
       printf("%d\n", sizeof(multi[0][0]));

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Here's one way.
    Code:
    #include <stdio.h>
    
    #define ROWS 3 
    #define COLS 4 
     
    int main() { 
    
    	int multi[ROWS][COLS] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
        
    	int * ptr1 ; 
    	
    	ptr1 = &multi[0][0];
    	int i , j ; 
    	
    	for (i = 0  ; i < ROWS  ; i++ ) { 
    		for (j = 0  ; j < COLS  ; j++ ) { 
    			printf("The value @ [&#37;d][%d] is %d\n", i,j,*ptr1 ) ; 
    			ptr1++ ; 
    		}
    	}
    	return 0 ; 
    }
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    That makes sense but....

    I am trying to learn how to use pointer in this fashion...int (*ptr)[4]. Is that not neccessary? The book I am reading uses the pointer that way but then does this... p= (int *)ptr; in a function and when I do it I get a compile error... Operation between types "int(*)[4]" and "int*" is not allowed. Could you give me an example doing it this other way? Thanks always for your help.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    No I can't - haven't done that yet.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > I know I can use multi[x][x]
    Try ptr[x][x] as well

    (*ptr)[x]
    would print the first row, x = 0 to 3

    *ptr being the equivalent of ptr[0]
    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. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 02:56 AM
  2. Problem within multi dimensional array
    By lolguy in forum C Programming
    Replies: 5
    Last Post: 12-26-2008, 07:02 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Printing an integer stored in an array
    By Freez3L in forum C Programming
    Replies: 4
    Last Post: 11-18-2002, 01:11 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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