Thread: Pointers and Arrays

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    8

    Pointers and Arrays

    hi there,
    small problem with the following code. i can not add two separate arrays. can you help.
    my compilier says ---- invalid operands to binary + ----


    *(*(c + i) +j) = *(*(a + i) +j) + *(*(a + i) +j) ;

    this is my problem line..............

    sean.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    *(*(c + i) +j) = *(*(a + i) +j) + *(*(a + i) +j) ;

    this is my problem line..............
    You can say that again. That's horrible code. It's a nightmare to read. Are you trying to add the values contained in a cell of the array, or what?

    Your variables hold no meaning what so ever, additionally, it's not clear at all what you're actually trying to do. Take this for example:

    *(*c+i)+j)

    What is this supposed to do?

    "*(c+i)" to me means "c is a character pointer, and i is an integer. "+j" Let's dereference that, and add the value of j to it. "*( ... )" Now, dereference that number. This is not what you mean to do ... is it?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    8
    sorry about the horrible code, just beginnig.
    my function readin is called twice. this function reads in vales into
    two separate arrays. function compute is meant to add these arrays together and finally printout prints all to screen.
    i have this working without pointers, i just cannot get it to work
    with pointers.

    sean.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Anything that I changed I just commented, so you could see what was changed.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include<conio.h>
    
    //int  rows = 20;
    //int cols = 30;
    int  rows;
    int cols;
    int i,j;
    
    //void readin( int *a[][cols], int rows, int cols);				/* declare function 'readin' */
    //void compute(int *a[][cols], int rows,int cols, int *b[][cols], int *c[][cols]);	/* declare function 'compute' */
    //void printout(int *a[][cols], int rows,int cols, int *b[][cols], int *c[][cols]);	/* declare function 'printout' */
    void readin( int **a, int rows, int cols);				/* declare function 'readin' */
    void compute(int **a, int rows,int cols, int **b, int **c);	/* declare function 'compute' */
    void printout(int **a, int rows,int cols, int **b, int **c);	/* declare function 'printout' */
    
    
    main()	/* start of main */
    { 
    	//int *a[rows][cols];	/* array 'a' */
    	//int *b[rows][cols];	/* array 'b' */
    	//int *c[rows][cols];	/* array 'c' */
    	int **a;	/* array 'a' */
    	int **b;	/* array 'b' */
    	int **c;	/* array 'c' */
    
    
    	printf("Please enter no of rows ");
    	scanf("%d",&rows);
    
    	printf("Please enter no of cols ");
    	scanf("%d",&cols);
    
            a = malloc(rows*sizeof(int *));
            b = malloc(rows*sizeof(int *));
            c = malloc(rows*sizeof(int *));
    
    
    		for ( i = 0 ; i < rows ; i++ )	/* memory allocation */
    		{
                       a[i] = malloc(cols*sizeof(int));
                       b[i] = malloc(cols*sizeof(int));
                       c[i] = malloc(cols*sizeof(int));
    			//for ( j = 0 ; j < cols ; j++ )
    			//{ 
    			//a[i][j] = (int*) malloc(cols * sizeof(int));
    			//b[i][j] = (int*) malloc(cols * sizeof(int));
    			//c[i][j] = (int*) malloc(cols * sizeof(int));
    			//}
    		}
    
    
    
    
    	clrscr();	/* clear screen */
    
    	readin( a, rows, cols);		/* call function 'readin' to obtain contents of array'a' */
    	readin( b, rows, cols);		/* call function 'readin' to obtain contents of array'b'*/
    	compute(a,rows, cols,b,c);	/* call function 'compute' */
    	printout(a,rows, cols,b,c);	/* call function 'printout' */
    	for ( i = 0 ; i < rows ; i++ )	/* memory allocation */
    	{
               free(a[i]);
               free(b[i]);
               free(c[i]);
            }
            free(a);
            free(b);
            free(c);
    
    
    
    
    }	 /* end of main */
    
    
    
    	//void readin(int *a[][cols], int rows, int cols)		 /* function 'readin' */
    	void readin(int **a, int rows, int cols)		 /* function 'readin' */
    	{
    		int i,j; 
    		printf("\nEnter values for Array \n\n");
    		for ( i = 0 ; i < rows ; i++ )	/* read  in array 'a' */
    		{ 
    			for ( j = 0 ; j < cols ; j++ ) 
    			{ 
    			printf( "%d,%d ",i, j ) ; 
    			scanf( "%d" ,(*(a + i) +j) ) ;
    			}
    		} 
     
    		clrscr();	/* clear screen */	
    
    	}	 /* end of function 'readin' */ 
    
    
    
    	//void compute(int *a[][cols], int rows, int cols, int *b[][cols], int *c[][cols])	/* function 'compute' */
    	void compute(int **a, int rows, int cols, int **b, int **c)	/* function 'compute' */
    	{
    		int i,j; 
    
    		for ( i = 0 ; i < rows ; i++ )	/* Compute contents of Array 'ca' */
    		{ 
    			for ( j = 0 ; j < cols ; j++ ) 
    			{ 
    			  //*(*(c + i) +j) = *(*(a + i) +j) + *(*(a + i) +j) ;
    			  *(*(c + i) +j) = *(*(a + i) +j) + *(*(b + i) +j) ;
    			}
    		} 
    		printf("\n");
    
    	}	/* end of function 'compute' */
    
    
    
    	//void printout(int *a[][cols], int rows, int cols, int *b[][cols], int *c[][cols])	/* function 'printout' */
    	void printout(int **a, int rows, int cols, int **b, int **c)	/* function 'printout' */
    	{
    		int i,j; 
    		printf("\tArray\t\tArray\t\tArray \n");
    		printf("\ta \t\tb\t\tc    \n");
    		printf("\t-------------------------------------\n");
    		for ( i = 0 ; i < rows ; i++ ) 	
    		{ 
    			 for ( j = 0 ; j <cols ; j++ ) 
    			{ 
    			printf("\t%d\t+\t%d\t =\t%d",*(*(a + i) +j), *(*(b + i) +j), *(*(c + i) +j) ) ; 	/* array 'a' + array 'b' = array 'c' */
    			printf("\n");
    			} 
    		} 
    		printf("\n");
    
    
    	}	/* end of function 'printout' */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM