"subscript requires array or pointer type"?
I tried to look for an answer using search the forum, but it hasn't been working for the last couple of days, unfortunately... :(
Maybe someone could point out where is the problem with this two dimensional array pointer? The problem arises in the function sumsquares, it supposed to take address of the array , rows and columns as parameters. However, compiler tells me there is no notation of an array pointer inside the function. I tried different things over the last 2 days and still nothing. What is the problem?
Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
/* function prototypes */
void GetData( int num[][4]);
double sumsquares ( int *, int, int );
int main( void )
{
int num[3][4] = {0};
int rows = 3;
int col = 4;
double sumsq = 0.0;
int *num_ptr;
num_ptr= &num[3][4];
GetData(num);
sumsq = sumsquares( &num[3][4], rows, col );
printf( "The sum of squares of each integer is: %.0f.\n", sumsq);
return 0;
}
/******************************* sumsquares() *********************************/
double sumsquares( int *num_ptr, int rows, int col )
{
double sum = 0;
double temp1 = 0;
double temp2 = 0;
double temp3 = 0;
for ( rows = 0, col = 0; col < 4; col++)
{
temp1 += pow( num_ptr[rows][col], 2.0 );
}
for ( rows = 1, col = 0; col < 4; col++)
{
temp2 += pow( num_ptr[rows][col], 2.0 );
}
for ( rows = 2, col = 0; col < 4; col++)
{
temp3 += pow( num_ptr[rows][col], 2.0 );
}
sum = temp1 + temp2 + temp3;
return sum;
}
/******************************* GetData() ***************************/
void GetData( int num[][4] )
{
int i = 0;
char instring[15] = {0};
printf( "Please enter the first row of four integers now: \n" );
for ( i = 0; i < 4; i++ )
{
printf( "#%2d: ", i + 1 );
num[0][i] = atoi( gets( instring ) );
}
printf( "Please enter the second row of four integers now: \n" );
for ( i = 0; i < 4; i++ )
{
printf( "#%2d: ", i + 1 );
num[1][i] = atoi( gets( instring ) );
}
printf( "Please enter the third row of four integers now: \n" );
for ( i = 0; i < 4; i++ )
{
printf( "#%2d: ", i + 1 );
num[2][i] = atoi( gets( instring ) );
}
}
Thanks in advance...