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.
Printable View
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.
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?Quote:
*(*(c + i) +j) = *(*(a + i) +j) + *(*(a + i) +j) ;
this is my problem line..............
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.
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.
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' */