Thread: 2d Arrays

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    2

    2d Arrays

    Hello there, im new to c-programming, and i ran accross a problem, a big big problem

    the task the program was suppose to do is to read a matrix from a file that's nxn, and create upper and lower matrices with it. However, i ran into trouble when i try to pass my array to a function to initialize it.

    im getting an error message of:
    subscripted value is neither array nor pointer.

    here is my code:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    void
    init_U (double *InputU, int n)
    {
    
    int i,j;
    
    
    for (i = 0; i < n; i++)
    {
    	for (j = 0; j < n; j++)
    		{
    			if (i != j)
    			{
    				*InputU[i][j] = 0; /*error here*/
    			}
    			else
    			{
    				*InputU[i][j] = 1; /*error here*/
    			}
    		}
    }
    }
    
    
    int
    main(void)
    {
    
    int n, temp_value;
    int i, j;
    double **A, **U, **L;
    FILE *inp;
    
    inp = fopen ("d1.txt", "r");
    fscanf (inp, "%d", &n);
    
    
    /*------------------------------------------------------------*/
    
    A = (double **) calloc (n,sizeof(double*));
    U = (double **) calloc (n,sizeof(double*));
    L = (double **) calloc (n,sizeof(double*));
    
    for (i = 0; i < n; i++)
    {
    A[i] = (double *) calloc (n, sizeof(double));
    U[i] = (double *) calloc (n, sizeof(double));
    L[i] = (double *) calloc (n, sizeof(double));
    }
    
    
    /*------------------------------------------------------------*/
    
    for (i = 0; i < n; i++)
    {
    	for (j = 0; j < n; j++)
    		{
    			fscanf(inp, "%lf", &A[i][j]);
    		}
    }
    
    /*------------------------------------------------------------*/
    
    init_U(&**U, n);
    
    /*------------------------------------------------------------*/
    fclose(inp);
    return(0);
    
    }
    Any help would be greatly appreciated. Thanks a lot in advance.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The reason you get an error is because your function prototype and usage is wrong. You have a pointer to a pointer, so make a function that takes that.
    Code:
    void foo( double **array, int bar )
    {
        ...do stuff...
    }
    
    ...do more stuff...
    
    double **myarray;
    ...do more more stuff...
    
    foo( myarray );
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    2
    oh my
    never saw that coming

    im still pretty newb when it comes to c programming

    thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 PM
  2. returning 2D arrays
    By ... in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 12:28 PM
  3. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM
  4. Reading 2d arrays from file?
    By Ringhawk in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2002, 09:05 PM
  5. how can i pass 2d arrays
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2001, 07:33 AM