Thread: Problems in passing 2-D array to a function

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    1

    Question Problems in passing 2-D array to a function

    I am learning to pass arrays to functions. My problem is this : I will scan a 2-D array in main() function. Pass the order n of the matrix and the address of first element to a function called pass(). Then I will print all the elements in pass().


    I cannot understand what is wrong with my code. Please point out my mistake.

    Code:
    # include<stdio.h>
    
    
    main()
    {
    	float a[50][50];
    	int i, j, n ;
    	void pass( int , float* ) ;
    	printf("Enter n : ") ;
    	scanf("%d", &n) ;
    
    
    	for( i = 0; i < n ; i++)
    	{
    		for( j = 0; j < n ; j++)
    		{
    			printf("\n Enter a%d%d = ", i, j) ;
    			scanf("%f", &a[i][j]) ;
    		}
    	}
    
    
    	pass(n, &a[0][0]) ;
    	
    }
    
    
    void pass( int n, float *a_pointer )
    {
    	int i, j ;
    	for( i = 0; i < n ; i++)
    	{
    		for( j = 0; j < n ; j++)
    		{
    			printf("\n\n %f", *(a_pointer + n*i + j )) ;
    		}
    	}
    }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Because *a_pointer can only be used for a 1D array.

    Here is a good link to get you started
    23.1: Multidimensional Arrays and Functions
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    That is odd. If you can live with fixed N, types are your friend:
    Code:
    enum { N = 50 };
    
    typedef struct {
        float data[N][N];
    } Float2d;
    
    void pass(const Float2d *grid)
    {
        int i, j;
        for (i = 0; i < N; ++i)
            for (j = 0; j < N; ++j)
                printf("%f\n", grid->data[i][j]);
    }
    grid->data knows about the layout so you don't have to. Of course, fixed N is probably not acceptable.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Your 2d array a is layed out 50 x 50, not n x n. That's your problem. This might help you see it:
    Code:
    int main()
    {
        float a[10][10] = {0};
        int i, j, n;
    
        printf("Enter n : ") ;
        scanf("%d", &n) ;
     
        for(i = 0; i < n ; i++)
        {
            for(j = 0; j < n ; j++)
            {
                printf("\n Enter a%d%d = ", i, j) ;
                scanf("%f", &a[i][j]) ;
            }
        }
    
        printf("\nMemory layout:\n");
        for( i = 0; i < n ; i++)
        {
            for( j = 0; j < n ; j++)
            {
                const float *p = &a[i][j];
                printf("i=%d j=%d p=%p floatsAway=%d\n", i, j, p, p - &a[0][0]);
            }
        }
    }
    Your code will work when n == 50.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by boral View Post

    Code:
    for( j = 0; j < n ; j++)
            {
                printf("\n\n %f", *(a_pointer + n*i + j )) ; /* Here  */
            }
      
    }
    What exactly are you trying to do here? You have some out of bounds arrays access.

    Code:
    printf("\n\n %f", *(a_pointer + n*i + j ));
                /* for say [3][3]
                /* 1st loop -> 0 , 1 , 2
                /* 2rd loop -> 3 , 4 , 5
                */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing values from function to main variables
    By ERJuanca in forum C Programming
    Replies: 18
    Last Post: 06-12-2009, 07:13 PM
  2. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 11:01 AM
  3. Problems passing parameter to function
    By HAssan in forum C Programming
    Replies: 5
    Last Post: 10-21-2008, 02:26 PM
  4. Passing address to a function, Problems !
    By WarDoGG in forum C Programming
    Replies: 4
    Last Post: 07-10-2008, 03:58 PM
  5. Problems passing an array to a function
    By ndenklau in forum C Programming
    Replies: 5
    Last Post: 09-20-2006, 08:14 AM