Thread: Pointer with Multi-dimensional Array

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    23

    Unhappy Pointer with Multi-dimensional Array

    Hi, all I am having a trouble with pointer and multi-dimensional array.
    I wrote the same thing but I see different output. below is the code

    Code:
    #include <stdio.h>
    
    void printarray_1 (int (*ptr)[4]);
    void printarray_2 (int (*ptr)[4], int n);
    
    int main(void)
    {
    	int multi[3][4] = {
    		{1,2,3,4},
    		{5,6,7,8},
    		{9,10,11,12}
    	};
    	/* ptr is a pointer to an array of 4 ints. */
    	int (*ptr)[4];
    	int *p;
    	int count = 0;
    
    	/* set ptr to the first element of multi */
    	ptr = multi;
    
    	/* with each loop, ptr is incremented to point to the nex */
    	/* element (that is, the next 4 element integer array) of multi. */
    	for ( count=0; count < 3; count++ )
    	{
    		printarray_1(ptr++);
    	}
    
    	puts("\n\nPress Enter...");
    	getchar();
    
    	printarray_2(multi, 3);
    	printf("\n");
    	
    	puts("\n\nPress Enter...again");
    	getchar();
    	
    	printf("\n\n");
    	
            /*
            from here it print out something crazy as you can see the output below.
            */
    	p = (int *) ptr; 
    	
    	for (count = 0; count < (4*3); count++)
    	{
    		printf("\n%d", *p++);
    	}
    	
    	printf("\n\n");
    	return 0;
    }
    void printarray_1 (int (*ptr)[4])
    {
    	/* Prints the elements of a single four-element integer array. */
    	/* p is a pointer to type int. You must use a type cast */
    	/* to make p equal to the address in ptr. */
    	int *p, count;
    	p =  (int *) ptr;
    
    	for ( count=0; count<4; count++ )
    		printf("%d\n", *p++);
    }
    
    void printarray_2 (int (*ptr)[4], int n)
    {
    	int *p, count;
    	p = (int *) ptr;
    	
    	for (count = 0; count < (4*n); count++)
    	{
    		printf("\n%d", *p++);
    	}
    
    }
    Here is the out put

    Code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    
    
    Press Enter...again
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    
    -858993460
    1245112
    4267446
    1
    3493392
    3486624
    2018114650
    1028656064
    29897040
    2147348480
    3579545
    0
    
    Press any key to continue . . .

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    When you assign p an address here:

    Code:
    p = (int *) ptr;
    what does ptr point to?

    Todd

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You increment ptr here:
    Code:
    >	for ( count=0; count < 3; count++ )
    >	{
    >		printarray_1(ptr++);
    >	}
    So after the above loop ptr points past the end of the array. So you could reset it back to the beginning of the array:
    Code:
    	ptr = multi;

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    23
    Quote Originally Posted by swoopy View Post
    You increment ptr here:
    Code:
    >	for ( count=0; count < 3; count++ )
    >	{
    >		printarray_1(ptr++);
    >	}
    So after the above loop ptr points past the end of the array. So you could reset it back to the beginning of the array:
    Code:
    	ptr = multi;
    I will try this,

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    23
    Quote Originally Posted by Todd Burch View Post
    When you assign p an address here:

    Code:
    p = (int *) ptr;
    what does ptr point to?

    Todd
    Since ptr is an integer pointer for array, and p is just a pointer to a normal integer value, thus in theory, we need to cast the value to integer before assigning the value.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Sorry, I wasn't asking a question for my edification. I was posing the question to you, so you could study your code, and realize that ptr was not pointing to where you intended.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    23

    Question

    Quote Originally Posted by swoopy View Post
    You increment ptr here:
    Code:
    >	for ( count=0; count < 3; count++ )
    >	{
    >		printarray_1(ptr++);
    >	}
    So after the above loop ptr points past the end of the array. So you could reset it back to the beginning of the array:
    Code:
    	ptr = multi;
    Yes, you are right, I made the changes as you told, and it works.
    However, I am not quite sure, why we need to reset it. because when we pass the pointer to the functions, in the functions, I did not reset it. and it works, but in main program, i have to reset it!!!!!!

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >because when we pass the pointer to the functions, in the functions, I did not reset it. and it works
    In both printarray_1 and printarray_2, you increment p, but not ptr (the passed pointer). But having said that, if you did increment ptr, this would not be seen back in main(), because in C, all parameters to functions are passed by value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. multi dimensional array at runtime
    By sawer in forum C++ Programming
    Replies: 21
    Last Post: 05-10-2006, 10:59 PM
  4. Multi Dimensional Array Compare
    By guitarist809 in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2006, 05:03 PM
  5. ? passing multi dimensional array to function
    By rc7j in forum C++ Programming
    Replies: 1
    Last Post: 02-10-2002, 02:19 AM