I have an array of int *arr[3];
and its elements are initialized like this :
sides[0] = &a;
sides[1] = &b;
sides[2] = &c;
Where a=b=5 and c=10. Now when i try to cast the type of the value (from int to double) of the contents of the memory address that each pointer is pointing at, the 3rd value shows up always as 0. Do you know why?
Here's what i'm doing:
double a,b,c;
a = (double) *sides[0];
b = (double) *sides[1];
c = (double) *sides[2];
printf("%lf %lf %lf",a,b,c);
The output
i get is:
5.0000 5.0000 0.0000
Here's the full code:
#include <stdio.h>
#include <math.h>
void populateSides(int *[3]);
void calculateTriangleType(int *[3]);
/*
* Function populateSides: Has 1 arg
* 1st arg: an array of int *.
* This function stores the 3 sides of a triangle
* given by the user into the sides array.
*/
void populateSides(int *sides[]) {
int i,a,b,c;
scanf(" %d",&a);
scanf(" %d",&b);
scanf(" %d",&c);
while((c <= b) || (c <= a)) {
scanf(" %d",&c);
}
sides[0] = &a;
sides[1] = &b;
sides[2] = &c;
}
/*
*/
void calculateTriangleType(int *sides[]) {
double a,b,c;
a = (double) *sides[0];
b = (double) *sides[1];
c = (double) *sides[2];
printf("%lf %lf %lf",a,b,c);
}
int main(void) { // Stelios Papamichail 4020
int *sides[3]; // stores each side of a triangle ( >0)
populateSides(sides);
calculateTriangleType(sides);
return 0;
}