1

FINAL EDIT: First of all, thank you guys for your patience. In the future I'll try to bring an MCVE to any questions I ask.

The program seems to be working fine now - I may have unwittingly deleted a typo or something.


This question annoys me quite a bit - I didn't choose to implement it this way, but it's required and I need to resolve it.

I've got a function that's supposed to read a piece of data from a struct, like this:

typedef struct {
    int data;
    char extraneous_stuff[];
} MyStruct;
...
//there's also a ParentStruct that holds a MyStruct array
...
void read_data(ParentStruct *parent, int which_MyStruct, int *data) {...

What I need to do is use the int *data as an out-parameter to copy the int data from inside a selected MyStruct. The problem I'm having, of course, is that both the out-parameter and the MyStruct field have exactly the same name. I found that trying to copy directly, i.e. *data = parent->array[which_MyStruct].data; compiled, but the data didn't actually get copied to the out parameter.

In order to hold to specifications I'm not supposed to change the names of the parameters here. Am I doomed?

EDIT: I included the following print statements inside the function:

printf("%d", *data);
printf("%d", parent->array[which_MyStruct].data); 

and, having put

int n = 0;
void read_data(*parent, which_MyStruct, &n)

I got back

0
0

as a result.

EDIT 2:

OK, this is very weird. I have this:

MyStruct current = parent->array[which_MyStruct];
printf("%s", current.extraneous_stuff[0]);
printf("%d", *data);
printf("%d", parent->array[which_MyStruct].data); 

and now suddenly "parent->array[which_MyStruct].data" has the appropriate value. Maybe this is undefined behavior, because the only change I made was to include the print statement "printf("%s", current.extraneous_stuff[0]);" and the output changed.

Chris
  • 215
  • 1
  • 12
  • 3
    it compiled because it's ok to do so. the copy failed because of something else. pls provide MCVE _https://stackoverflow.com/help/mcve_ – CIsForCookies Jun 20 '17 at 05:26
  • 2
    There issue is something different and it not relevant to naming convention. – Kanji Viroja Jun 20 '17 at 05:28
  • Yes @KanjiViroja is correct. Naming convention is ok. Try to print *data and array[].data variables locally inside the read_data to check properly assigned or not – Rajeshkumar Jun 20 '17 at 05:30
  • @Rajeshkumar I was just going to edit that in, hang on one sec – Chris Jun 20 '17 at 05:31
  • 1
    show how `parent` is declared *and* initialized. See: [**How to create a Minimal, Complete, and Verifiable example**](http://stackoverflow.com/help/mcve). – David C. Rankin Jun 20 '17 at 05:36
  • @DavidC.Rankin I'm sorry about that; the issue is that this is schoolwork, so I'm not allowed to post the actual code. It might take me a while to generate the MCVE. – Chris Jun 20 '17 at 05:37
  • There are a lot of things that depend on your declaration of `ParentStruct` and what you pass as `parent` itself. I commend you though for taking you non-post obligations seriously, but understand it does leave us guessing a bit.... (we can't see your screen from here `:)` – David C. Rankin Jun 20 '17 at 05:39
  • @Chris, as you can see your `parent->array[which_MyStruct].data` is already 0. So *data is also set to 0. If you expect some other value, you should look at the part of the code where parent is created/initialized. – Ajay Brahmakshatriya Jun 20 '17 at 05:40
  • 1
    For help with debugging see https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and https://stackoverflow.com/questions/2069367/how-to-debug-using-gdb – Yunnosch Jun 20 '17 at 05:47
  • "*there's also a ParentStruct that holds a MyStruct array*" -- that wouldn't make sense with the struct containing a flexible array member ... maybe you mean an array of pointers? –  Jun 20 '17 at 05:48
  • @Yunnosch thank you for the resources, I've bookmarked both. – Chris Jun 20 '17 at 05:56
  • 2
    You can't have an array of variable-length structures. – user253751 Jun 20 '17 at 05:58
  • @immibis Yes, I should've written extraneous_stuff[MAX_STUFF_SIZE]. – Chris Jun 20 '17 at 06:10

0 Answers0