Note that there are some explanatory texts on larger screens.

plurals
  1. POPointer isn't pointing
    text
    copied!<p>I have this 'ere code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; // Library stuff typedef struct { int x, y; } _TYPE_position; typedef struct { char image[32]; _TYPE_position position; } _TYPE_object; _TYPE_object *object; int createObject(const _TYPE_object *insertionObject); int createObject(const _TYPE_object *insertionObject) { int index; _TYPE_object *newObjectPtr = realloc(object, sizeof(*object) + sizeof(_TYPE_object)); if(newObjectPtr != NULL) { object = newObjectPtr; index = sizeof(*object) / sizeof(_TYPE_object) - 1; strcpy(object[index].image, insertionObject-&gt;image); object[index].position = insertionObject-&gt;position; } else { index =- 1; } return index; } // Demo program int main(void) { _TYPE_object smile = { "smile.png", { 112, 80 } }; int smileIndex = createObject(&amp;smile); if(smileIndex == -1) { printf("Error allocating memory for smile object"); while(1); } smile.position.x = 55; // Does nothing since the object does not point here for some reason int i; for(i = 0; i &lt; sizeof(*object) / sizeof(_TYPE_object); i++) { printf("Create %s at { %d, %d }\n", object[i].image, object[i].position.x, object[i].position.y); } return 0; } </code></pre> <p>And it kind of works. It seems that object is not pointing to a _TYPE_object but is instead creating an exact copy of it.</p> <p>Is there a way to make the object array point to objects, rather than just recreating them. So in the above example have smile.position.x = 55; actually change the value that gets printed.</p>
 

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