I have this code here that I think should work, and it does! except for the fact that though I've declared a pointer to an array of type double, it always stores int, and I don't know why.
first, I have this struct and it is defined like this:
struct thing {
int part1;
double *part2;
};
then I initialize the thing by saying struct thing *abc = malloc (sizeof(struct thing))
and part1 = 0
and part2 = malloc(sizeof(double))
.
then, I try to set specific values at specific positions in the array of double. This works fine with integers, but when I tried 0.5, it set the value to 0. when I tried 2.9, it set the value to 2. I really don't know why it does this. code for setValue looks like this:
struct thing *setValue (struct thing *t, int pos, double set){
if (t->part1 < pos){ // check to see if array is large enough
t->part2 = realloc (t->part2, (pos+1) * sizeof(double));
for (int a = t->part1 + 1; a < pos + 1; a++)
t->part2[a] = 0;
t->part1 = pos;
}
t->part2[pos] = set; // ALWAYS stores an integer, don't know why
return t;
}
-- Edit: So there is nothing really mallicious about this part; but here's the rest of my code JUST IN CASE:
Relevant functions that operate on my struct thing
#include "thing.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
struct thing *makeThing(){ // GOOD
struct thing *t = (struct thing *) malloc (sizeof(struct thing));
t->part1 = 0;
t->part2 = malloc (sizeof(double));
t->part2[0] = 0;
return t;
}
struct thing *setValue (struct thing *t, int pos, double set){
if (t->part1 < pos){ // check to see if array is large enough
t->part2 = realloc (t->part2, (pos+1) * sizeof(double));
for (int a = t->part1 + 1; a < pos + 1; a++)
t->part2[a] = 0;
t->part1 = pos;
}
t->part2[pos] = set; // ALWAYS stores an integer, don't know why
return t;
}
double getValue (struct thing *t, int pos){
if (pos <= t->part1){
return t->part2[pos];
}
return 0;
}
Header file:
#ifndef THING_H
#define THING_H
struct thing {
int part1;
double *part2;
};
struct thing *makeThing();
struct thing *setValue (struct thing *t, int pos, double set);
double getValue (struct thing *t, int pos);
#endif
main file:
#include <stdio.h>
#include "thing.h"
int main (void)
{
struct thing *t = makeThing();
setValue (t, 1, -1);
setValue (t, 1, -2);
setValue (t, 10, 1);
setValue (t, 3, 1.5);
printf ("%g\n", getValue (t, 0));
printf ("%g\n", getValue (t, 1));
printf ("%g\n", getValue (t, 2));
printf ("%g\n", getValue (t, 3));
printf ("%g\n", getValue (t, 4));
printf ("%g\n", getValue (t, 5));
printf ("%g\n", getValue (t, 6));
printf ("%g\n", getValue (t, 7));
printf ("%g\n", getValue (t, 8));
printf ("%g\n", getValue (t, 9));
printf ("%g\n", getValue (t, 10));
return 0;
}
On my computer, this prints out: 0 -2 0 1 0 0 0 0 0 0 1
EDIT: Turns out that when I compile it via codeblocks, it works...
Ultimately, I am confused.
Double converts to int in C?
No, it doesn't. When you assign a double
value to an object ot type double
there is no conversion whatsoever.
Your problem is not in the code you've shown; it is somewhere else (the way you print, some stupid #define
, some other thing).
Oh! And you really should make sure the realloc()
s work. Otherwise, instead of an error, users may get a slightly erroneous value...
As I said in a comment, your code works as expected at ideone .
I think you might have messed up your format specifier in your printing. Be sure to have the correct specifiers for the correct data types.
I know that happened to me sometimes when I changed data types in my Objective-C code. :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.