Kinetis K40 codewarrior structure problem

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Kinetis K40 codewarrior structure problem

Jump to solution
1,304 Views
mareklagger
Contributor III

Hello,

I am using eclipse based codewarrior for mcu 10.1 and I cant use structures, what Iam doing wrong?

struct steering {

     int P;

};

steering left;

left.P=0;

typedef struct{

     int x;

     int y;

} new_struct;

new_struct.x=0;

In both examples I am getting declaration syntax error. It can make a structure but if I want to change its member I am getting declaration syntax error.

Thank You

0 Kudos
Reply
1 Solution
933 Views
pgo
Senior Contributor V

Hi Carlos,

Apart from the colon instead of a semi-colon, this compiles fine for me with CW10.1.

For reference - entire main.c:

#include <stdio.h>

#include "derivative.h" /* include peripheral declarations */

typedef struct {

     int x;

     int y;

} new_struct;

new_struct my_new_struct;

void main (void) {

   my_new_struct.x=0;

}


View solution in original post

0 Kudos
Reply
6 Replies
933 Views
pgo
Senior Contributor V

Hi,

There are a couple of obvious problems.

You may be used to C++ which has some different rules to C.

Try:

struct steering {

   int P;

};

struct steering left;

left.P=0;

You need to repeat the keyword struct

in the declaration of the variable.

typedef struct

int x;

int yl

}new_struct;

new_struct.x = 0;

This is illegal for two reasons:

typedef struct does not create a variable but a type instead.


You also cannot have declarations after executable statements e.g.


int x;

x = 4;

int y;  /* Illegal */



bye

933 Views
mareklagger
Contributor III

I tried it and the result is the same. Declaration and syntax error for both. It looks like there is something with compiler.

0 Kudos
Reply
933 Views
carlos_neri
NXP Employee
NXP Employee

Could you please try the following:

typedef struct{

     int x;

     int y;

} new_struct;

new_struct my_new_struct:

void foo (void)

{

my_new_struct.x=0;

}

0 Kudos
Reply
934 Views
pgo
Senior Contributor V

Hi Carlos,

Apart from the colon instead of a semi-colon, this compiles fine for me with CW10.1.

For reference - entire main.c:

#include <stdio.h>

#include "derivative.h" /* include peripheral declarations */

typedef struct {

     int x;

     int y;

} new_struct;

new_struct my_new_struct;

void main (void) {

   my_new_struct.x=0;

}


0 Kudos
Reply
933 Views
mareklagger
Contributor III

Thank you. The problem was that I didnt create the variable of struct type. But there was also some minor problem because I declared it in header. Now its working well

0 Kudos
Reply
933 Views
carlos_neri
NXP Employee
NXP Employee

Thanks for the feedback PGO.

Hopefully this will help Marek.

0 Kudos
Reply