Note that there are some explanatory texts on larger screens.

plurals
  1. PODouble array only stores ints
    primarykey
    data
    text
    <p>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.</p> <p>first, I have this struct and it is defined like this:</p> <pre><code>struct thing { int part1; double *part2; }; </code></pre> <p>then I initialize the thing by saying <code>struct thing *abc = malloc (sizeof(struct thing))</code> and <code>part1 = 0</code> and <code>part2 = malloc(sizeof(double))</code>.</p> <p>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:</p> <pre><code>struct thing *setValue (struct thing *t, int pos, double set){ if (t-&gt;part1 &lt; pos){ // check to see if array is large enough t-&gt;part2 = realloc (t-&gt;part2, (pos+1) * sizeof(double)); for (int a = t-&gt;part1 + 1; a &lt; pos + 1; a++) t-&gt;part2[a] = 0; t-&gt;part1 = pos; } t-&gt;part2[pos] = set; // ALWAYS stores an integer, don't know why return t; } </code></pre> <p>-- Edit: So there is nothing really mallicious about this part; but here's the rest of my code JUST IN CASE:</p> <p>Relevant functions that operate on my struct thing</p> <pre><code>#include "thing.h" #include &lt;math.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; struct thing *makeThing(){ // GOOD struct thing *t = (struct thing *) malloc (sizeof(struct thing)); t-&gt;part1 = 0; t-&gt;part2 = malloc (sizeof(double)); t-&gt;part2[0] = 0; return t; } struct thing *setValue (struct thing *t, int pos, double set){ if (t-&gt;part1 &lt; pos){ // check to see if array is large enough t-&gt;part2 = realloc (t-&gt;part2, (pos+1) * sizeof(double)); for (int a = t-&gt;part1 + 1; a &lt; pos + 1; a++) t-&gt;part2[a] = 0; t-&gt;part1 = pos; } t-&gt;part2[pos] = set; // ALWAYS stores an integer, don't know why return t; } double getValue (struct thing *t, int pos){ if (pos &lt;= t-&gt;part1){ return t-&gt;part2[pos]; } return 0; } </code></pre> <p>Header file:</p> <pre><code>#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 </code></pre> <p>main file:</p> <pre><code>#include &lt;stdio.h&gt; #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; } </code></pre> <p>On my computer, this prints out: 0 -2 0 1 0 0 0 0 0 0 1</p> <p>EDIT: Turns out that when I compile it via codeblocks, it works...</p> <p>Ultimately, I am confused.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload