-1

In my code I have a struc

struct Test {
    int a;  
    int b;
    char c;
};

With my function:

int TestFunction(void* ptr){
    struct Test test;
    test.a = 0;
    test.b = 1;
    strcpy(c,"hello");

    return 0;
    }

Now to link the temp struc to the void ptr I passed in I have

struct Test* temp = (struct Test*)ptr;
struct Test test = *temp;

Is this the correct way to link strucs with void ptrs? Is there an easier way?

chonglawr
  • 201
  • 1
  • 3
  • 15
  • "*Is this the correct way to link strucs with void ptrs?*" -- It is *a* way. Whether it is the correct way depends on the results you are expecting. Are you expecting modifications to the struct to be visible in the struct pointed to? – Dolda2000 Sep 12 '16 at 00:46
  • Do you want to modify the original struct that was passed in or only a copy of it? And what is `strcpy(c,"hello");` meant to do as `c` is undeclared? Also, `void` pointers can be assigned to any other pointer type. So the cast is not needed and thus the simpler: `struct Test* temp = ptr;` – kaylum Sep 12 '16 at 00:46
  • @Dolda2000, yep I want to modify the void ptr struc when the function returns – chonglawr Sep 12 '16 at 00:51
  • @kaylum the char is a typo, but thanks for the info about the simplification – chonglawr Sep 12 '16 at 00:52
  • We meant do you want the struct modifications *inside* the function to be on the original struct or a copy? That is, do you want `test.a = 0;` to make the original struct's `a` field be set to 0? – kaylum Sep 12 '16 at 00:54
  • @kaylum yes whatever is done in the function would modify the original struc – chonglawr Sep 12 '16 at 00:57
  • ok, then your code as shown does not achieve that as it operates on a *copy* of the original struct. The answer from @Dolda2000 is what you are looking for. – kaylum Sep 12 '16 at 00:59
  • By the way, you seem to be using the word "struc" consistently. The correct term is "struct". – Dolda2000 Sep 12 '16 at 01:04

1 Answers1

1

Since you want to be able to modify the struct pointed to, your sample code is not appropriate. What it does is create a local copy of the pointed-to struct and modifies the local copy instead of the original object.

What you want to do, rather, is this:

int TestFunction(void *ptr) {
    struct Test *test = ptr;
    test->a = 0;
    test->b = 1;

    return 0;
}

The a->b syntax is equivalent to (*a).b, meaning that it refers to whatever test points to.

Dolda2000
  • 25,216
  • 4
  • 51
  • 92
  • thanks! Would you be able to show an example with arrays and strings too? would it look like test->arr = myarr, assuming myarr is an array declared and test->str = mystring? – chonglawr Sep 12 '16 at 01:01
  • @chonglawr: It works exactly the same as for a local variable of the type you want to modify. As for arrays, you can never assign to arrays, be they local variables or struct members. – Dolda2000 Sep 12 '16 at 01:02
  • Just to clarify, why wouldn't my original code work, when I create temp and point it to ptr, in test I am getting the value of temp, isn't that the same as what you have in you set test equal to temp and you deref for the struct? – chonglawr Sep 12 '16 at 01:06
  • @chonglawr: Because your `test` is a local variable. Doing `struct Test test = *temp;` creates a local variable `test` and *copies* the contents of the struct that `temp` points to into it. Essentially, at this point you have two `struct Test` objects: one that `ptr` and `temp` point to, and one that `test` has allocated in the current stack frame. Your modifications go to the latter, and the former is left unchanged. – Dolda2000 Sep 12 '16 at 01:09