0

I am trying to access data in the structure type function through a pointer. When i do it, i am getting 3 errors

#79 expected a type specifier
#159 declaration is incompatible with previous "memcmp"

Header file:

typedef struct 
{

   uint8 a[50];

   uint8 b;

   uint8 c;
} get;

.c file:

main.c()
{

    get example[3];
    get* example(void)
      {
        uint_8 l_LoopCounter_u8;
        example1_st.a[l_LoopCounter_u8++] = data;
        example.b = data;
        example.c = data;
        return (void*)&example1_st ;
       }
}
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • First cleanup the code to make it readable. Then debug it. Part of debugging is making a [mcve]. Most other debugging is getting information. Introduce output with helpful information. Analyse it. Change code which outputs unexpected data. That is basically the way to fix errors. – Yunnosch Jan 26 '19 at 14:51
  • Your code file is incomplete. Show a MCVE pleasd. Add comments to indicate which the lines with the errors are. – Yunnosch Jan 26 '19 at 14:52
  • 1
    Your array and your function hace the same name, change one of them. – Yunnosch Jan 26 '19 at 14:53
  • You are defining a nested function, only do that if it is necessary and you can explain the reason in detail. If not, move it outside of the enclosing function. – Yunnosch Jan 26 '19 at 14:54
  • 1
    I guess that your function `main.c()` should be `int main(void)`. This kind of errors can be fixed by first learning to make a HelloWorld run. – Yunnosch Jan 26 '19 at 14:55
  • What is `example1_st`? – alk Jan 26 '19 at 14:55
  • 2
    Nested functions are not permitted in standard C (see https://stackoverflow.com/questions/2608158/nested-function-in-c). What is `data`? Where is line 159 (I don't see memcmp in the code you provided)? I find it hard to believe that you are only getting three errors. The types of error's I'm seeing here suggest that you have never programmed in C before. I would suggest finding a good book on C and reading it. – thurizas Jan 26 '19 at 15:01
  • By the way, please take the [tour]. There are many users here who feel much more helpful if you demonstrate that you spent at least that much effort. – Yunnosch Jan 26 '19 at 15:10

1 Answers1

0

Here is code which fixes the errors you ask about and a few others. Without a MCVE it is hard to get it completely working. You have some design changes coming up, to make the function work on any kind of variable, not only globals. Making a nested function is not a good way to make a local variable accessable in a funciton. Get the code working on a global, then change to work on call-by reference variables. The ++ indicate a more complex plan you are following. Do that later.

get example1_st[3]; // changed to the obviously intended name,
// should fix 'declaration is incompatible with previous "memcmp"'
// made this a global, to keep it accessable from function,
// make the code work, then refactor to have the function work on
// variables via call-by reference parmeters

// avoid nested function definition
get* example(void)
{
    uint_8 l_LoopCounter_u8=0; // init index variable

    example1_st.a[l_LoopCounter_u8] = data; // removed ++, which is unnclear/risky here
    example1_st.b[l_LoopCounter_u8] = data;
    example1_st.c[l_LoopCounter_u8] = data;
    return &example1_st ; // no need to cast to void, type is correct
}

int main(void) //changed to a correct main function head,
// should fix "expected a type specifier"
{

   // note that your main function was functionally empty anyway...

}

Note:
No, I did not test this code. It would be hard for lack of a MCVE by OP.
This is meant to help with problems, not deliver working code for unknown purpose.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54