# C Struct Pointer Binding Passes Wrong Values

**URL:** <https://forum.crystal-lang.org/t/c-struct-pointer-binding-passes-wrong-values/4236>\
**Category:** Help & Support\
**Created:** [January 5, 2022, 3:11pm UTC](https://forum.crystal-lang.org/t/c-struct-pointer-binding-passes-wrong-values/4236 "2022-01-05T15:11:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jay1](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jay1/32/1486_2.png) [@Jay1](https://forum.crystal-lang.org/u/Jay1)\
**Post date:** [January 5, 2022, 3:11pm UTC](https://forum.crystal-lang.org/t/c-struct-pointer-binding-passes-wrong-values/4236/1 "2022-01-05T15:11:15Z")

</div>

If I have the following C code:

```auto
typedef struct {
    int i;
  } Info;

typedef struct {
            int type;
             Info* info;
             } Data;

Data* myFunc() {
  Data *d = (Data*)malloc(sizeof(Data));

  Info in;
  in.i = 5 ;
  Info *s = &in;
  d -> type = 1;
  d -> info = s;
  //printf("Value: %d\n", ((Info*)(d -> info))->i );
  return d;
}

```

And matching Crystal code:

```auto
@[Link(ldflags: "#{ __DIR__ }/functions.o")]
lib LibCTest

  struct Data
    type : Int32
    info : Info*
  end

  struct Info
    i : Int32
  end

  fun myFunc() : Data*

end

p LibCTest.myFunc().value.info.value.i

```

I build the library with: gcc -Wall -O3 -march=native -c functions.c -o functions.o

The correct value of info.i is never passed i.e., 5. It will be passed if I uncomment the printf and re-compile strangely. This also happens if the struct Info pointer is void\* instead.

Any thoughts on what I’m doing wrong, I can’t find any posts or documentation on embedded C struct pointers.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [January 5, 2022, 3:22pm UTC](https://forum.crystal-lang.org/t/c-struct-pointer-binding-passes-wrong-values/4236/2 "2022-01-05T15:22:32Z")

</div>

The C implementation of `myFunc` has a bug: `info` is a pointer and you assign a stack pointer to it and return it from the function. But that pointer becomes invalid when leaving the scope of `myFunc`. It might still work in some cases, like the `printf` expression shows, but that’s just lucky.  
You’ll be able to observe the same behaviour calling `myFunc` from C.

To fix this, you could allocate `*s` on the heap, just like `*d`.
