Reputation: 1012
If I leave all variables as int I get 32. The division is supposed to give me 32.5 so I thought that changing everything to double would do it, but it just gives me zero...
Here is the code (everything in int):
#include <stdio.h>
#include <stdlib.h>
void sommeTableau(int tableau[], int tailleTableau, int *x, int *average);
int main(int argc, char *argv[])
{
int tableau[4] = {30, 50, 50};
int *x = 0;
int *average = 0;
const int tailleTab = 4;
sommeTableau(tableau, tailleTab, &x, &average);
printf("The average is %d\n", average);
return 0;
}
void sommeTableau(int tableau[], int tailleTableau, int *x, int *average)
{
int i = 0;
for (i = 0 ; i < tailleTableau ; i++)
{
*x = *x + tableau[i];
}
*average = *x/tailleTableau;
}
So this works and gives me 32... Now if I change everything to double and %d to %f in the printf function, it gives me zero and I don't understand why...
Upvotes: 0
Views: 337
Reputation: 58271
There is lots of mistakes in your code. I corrected all of them
#include <stdio.h>
#include <stdlib.h>
void sommeTableau(int tableau[], int tailleTableau, int *x, double *average);
int main(int argc, char *argv[])
{
int tableau[4] = {30, 50, 50};
int x = 0;
double average = 0;
const int tailleTab = 4;
sommeTableau(tableau, tailleTab, &x, &average);
printf("The average is %lf\n", average);
return 0;
}
void sommeTableau(int tableau[], int tailleTableau, int *x, double *average)
{
int i = 0;
for (i = 0 ; i < tailleTableau ; i++)
{
*x = *x + tableau[i];
}
*average = (double)(*x)/(double)tailleTableau;
}
Upvotes: 3
Reputation: 36494
Something is terribly wrong here -- you're defining x
and average
as pointer variables in main
, which if you are lucky is the same size as an int
, but may result in undefined behavior.
Remove the *
from the local variable declarations of x
and average
in main
and it may actually work.
Also, in sommeTableau
, both operands of the division (*x
and tailleTableau
) are int
, so integer division is performed and the remainder discarded prior to assignment to *average
.
Upvotes: 1
Reputation: 881383
One of your problems lies here:
int *x = 0;
int *average = 0;
sommeTableau(tableau, tailleTab, &x, &average);
Those first two lines should be:
int x = 0;
int average = 0;
This is evident from the warnings you should be getting, assuming you're using a decent compiler:
qq.c: In function ‘main’:
qq.c:13: warning: passing argument 3 of ‘sommeTableau’ from incompatible pointer type
qq.c:4: note: expected ‘int *’ but argument is of type ‘int **’
qq.c:13: warning: passing argument 4 of ‘sommeTableau’ from incompatible pointer type
qq.c:4: note: expected ‘int *’ but argument is of type ‘int **’
In addition, you don't need _everything to be a double, just the average, so you should only change the type of that variable and match that with the parameters passed to the function.
It will also be necessary to cast the total *x
to a double before doing the division, so that it knows you don't mean integer division.
With those changes, the output changes from:
The average is 4.000000
(for me) to the correct:
The average is 32.500000
See the following program for more detail:
#include <stdio.h>
#include <stdlib.h>
void sommeTableau(int tableau[], int tailleTableau, int *x, double *average) {
int i;
for (i = 0 ; i < tailleTableau ; i++)
*x = *x + tableau[i];
*average = ((double)(*x))/tailleTableau;
}
int main (void) {
int tableau[4] = {30, 50, 50};
int x = 0;
double average = 0;
const int tailleTab = 4;
sommeTableau(tableau, tailleTab, &x, &average);
printf("The average is %lf\n", average);
return 0;
}
Upvotes: 2
Reputation: 4365
You are passing x and average as null pointer. Thats why your code is giving segmentation fault.
As per you describtion please check(http://codepad.org/z8fVUTe5).
#include <stdio.h>
#include <stdlib.h>
void sommeTableau(int tableau[], int tailleTableau, int *x, int *average);
int main(int argc, char *argv[])
{
int tableau[4] = {30, 50, 50};
int x = 0;
int average = 0;
const int tailleTab = 4;
sommeTableau(tableau, tailleTab, &x, &average);
printf("The average is %d\n", average);
return 0;
}
void sommeTableau(int tableau[], int tailleTableau, int *x, int *average)
{
int i = 0;
for (i = 0 ; i < tailleTableau ; i++)
{
*x = *x + tableau[i];
}
*average = *x/tailleTableau;
}
Output:-
The average is 32
Upvotes: 2
Reputation: 62048
It doesn't actually work.
Here you define x
and average
as NULL
pointers to int
:
int *x = 0;
int *average = 0;
And here instead of pointers to int
you pass pointers to pointers to int
:
sommeTableau(tableau, tailleTab, &x, &average);
Which is clearly wrong. You should enable compiler warnings to see such problematic places and correct them.
Here the correction would be:
int x = 0;
int average = 0;
Now, here:
printf("The average is %d\n", average);
You are lying to printf()
about what you're going to give it (an int
, because of %d
), but you're actually giving it a pointer to int
. The above correction fixes this. If you lie to printf()
about the type of some parameter, anything can happen. If your compiler is gcc
and you enable warnings, you can spot this kind of problems as well.
Upvotes: 1
Reputation: 490108
int *x = 0;
int *average = 0;
This is defining two pointers and setting the pointers to zero -- which, in a pointer context translates to a null pointer. So, you have to pointers to nothing.
sommeTableau(tableau, tailleTab, &x, &average);
Here, you're passing the addresses of those pointers to the function, so the function is really receiving an int **
.
printf("The average is %d\n", average);
This is then taking the value of the pointer -- the address its holding -- and (probably) treating that bit pattern as an int
. This is technically undefined behavior, but on many machines, a pointer it enough like an int for it to appear to work. The same will almost never be true with a floating point number though.
What you want to do here is define two int
s (or two double
s) and pass their addresses to your function:
double x=0.0, average=0.0;
sommeTableau(tableau, tailleTab, &x, &average);
// ...
printf("%f, %f\n", x, average);
Upvotes: 1