The array in your function is a pointer. The array in your main is not. In other words: show more code.
Printable View
The array in your function is a pointer. The array in your main is not. In other words: show more code.
in main i have this:
float average = avg(&array[0][0], row, col);
and in the avg function i have this:
value = (int)(*(array+i));
calculation += value;
But calculation is not being set
Remember, in your function you have to do something like:
since &array[0][0] is a pointer-to-int (not a pointer-to-pointer-to-int). If you just want to loop over all the entries, then you could use real_array[0] through real_array[row*col-1].Code:int *real_array = (int *)array;
Ah i found the problem, its with the declaration type of calculation
xxsfdfd
calculation += value works whether or not calculation is a float. Perhaps value is 0? Print it out and see.
what about if I have two ints divided by each other like this:
average = (calculation/count);
where average is a float. It doesn't seem to give an answer, just 0.
Ah i fixed it, its %lf and not %d
thanks
Why not show the entire code?
I smell something bad. Maybe.
Take a lookCode:#include <stdio.h>
float avg(int **array, int row, int col)
{
//LOOP i
//LOOP j
//Take value of i,j and add to variable calculation
//Add 1 to count
//ENDLOOP
//ENDLOOP
//average <- calculation/count
//return average
int i,j, count=0, value, calculation=0;
float average=1.0;
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
value = (int)(*(array+i));
printf("Array Value = %d\n",value);
calculation += value;
printf("Calculation = %d\n",calculation);
//count++;
}
}
printf("Calculation FINAL = %d, COUNT = %d\n",calculation, count);
count = row*col;
average = (calculation/count);
printf("Average = %lf\n",average);
return average;
}
int main(void)
{
int i, j;
int row=3, col=3, array[row][col];
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
printf("Enter value %d,%d \n", i, j);
scanf("%d",&array[i][j]);
}
}
float average = avg(*array, row, col);
printf("Average = %lf\n",average);
}
My intuition does not fail me...
Your array is 2D-array. A 2D-array is NOT T**.
You also dereference the array, which will give you T*, not T**.
And you also treat your ptp as a single pointer.
You should probably take int* in your function, and cast the array to int*, and then use the array[n] syntax instead of your horrible syntax you have now.
Example:
Code:int main()
{
int array[10][10];
int* p = (int*)array; // Cast to a single pointer
for (int i = 0; i < rows; i++)
p[i] = ...;
//etc
}
hmm i see this is confusing stuff, i get the basics but with double pointers (**) i get confused. Do you know of a good tutorial as iv searched online and still a bit confused, couldnt find a good one to explain things such as why cast and why do you need 2 stars etc etc.
thanks
It's not a difficult concept.
Pointers being variables, they too, have an address.
And the way pointers work, they have the type that point to, plus an extra "*" to indicate it's a pointer. So if you take the address of a pointer, what do you get? That's right, you get a ptp (pointer-to-pointer).
But you should understand that while arrays decays to a pointer, a 2D array cannot decay to a ptp. The simple reason for that is that the compiler cannot calculate the correct offset in a 2D array with just a T**.
The real types becomes T (*)[n], where n is the size of the outermost dimension. It's a pointer to an array (pointer to a 1D array).