Author Topic: C-code; array of struct ? (Atmel Studio 6)  (Read 47679 times)

0 Members and 1 Guest are viewing this topic.

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
C-code; array of struct ? (Atmel Studio 6)
« on: July 30, 2013, 07:19:59 am »
I want to create an array of structures to hold button info for my LCD project.

Code: [Select]
typedef struct
{
unsigned int top;
unsigned int left;
unsigned int height;
unsigned int width;
char *caption;
}Button;

struct Button Buttons[4];

But that code gives me an error:

Code: [Select]
array type has incomplete element type
After Googling for some time, I'm none the wiser as to the correct syntax.   :(

Can anyone help please?
 

Offline ve7xen

  • Frequent Contributor
  • **
  • Posts: 732
  • Country: ca
    • VE7XEN Blog
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #1 on: July 30, 2013, 07:25:26 am »
You have typedef'd an annonymous struct. The 'struct' keyword in your array definition isn't necessary; the compiler is looking for a named struct called Button, not a type called Button.

IOW the compiler is expecting:
Code: [Select]
struct Button {
...
};
73 de VE7XEN
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7805
  • Country: us
  • adieu
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #2 on: July 30, 2013, 07:26:13 am »
Damn! Beat me to it...

What you have done is taken "struct {blah}" (an anonymous structure) and named it Button. Not 'struct Button', just Button. Lose the typedef and just say "struct Button {blah}".

The confusion may be this: typedef and struct are completely separate. struct creates a structure type, which can be named or anonymous. typedef creates an alias - you can even say "typedef unsigned short myfavoritenumbertype;". They are often used together but they are unrelated. You can "define" a type entirely without typedef.
« Last Edit: July 30, 2013, 07:32:27 am by c4757p »
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline amyk

  • Super Contributor
  • ***
  • Posts: 6821
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #3 on: July 30, 2013, 07:35:56 am »
Thus, you can now declare an array of 4 Button's as follows:
Code: [Select]
Button Buttons[4];
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #4 on: July 30, 2013, 10:45:07 am »
The only combination i can get to compile is:

Code: [Select]
struct Button
{
unsigned int top;
unsigned int left;
unsigned int height;
unsigned int width;
char *caption;
};

struct Button Buttons[4];

I needed to add the struct keyword to my draw procedure:

Code: [Select]
void Draw_Button(struct Button Btn);
But it does compile.  I'll be able to test it at work tomorrow.
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #5 on: July 30, 2013, 10:48:44 am »
Another question if i may; on an AVR, where would this data reside?

Would if be pulled into RAM at startup?  If so, that means those values could be modified during code execution right?
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7597
  • Country: nz
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #6 on: July 30, 2013, 11:00:22 am »
It will be in ram. If you initialize it to a value then that value will be stored in flash and get loaded into ram as needed.

Use the PROGMEM directive if you want access to readonly data without taking up space in ram. It means the data will be read directly out of flash whenever its needed (a little slower but gives you a crazy large amount of space compared to ram ).   

The internal EEPROM is only ever read/written when you actually use the eeprom read/write functions or use the EEMEM directive.
Also note...
- You can tell avrdude to use the last eeprom address as a counter for how many chip erase cycles have been performed. (So some programmers may do this by default, best to avoid this address)
- The first eeprom location 0x00 has been known to get corrupted in some situations, its best not to use it.
- The EEMEM directive lets the compiler pick where to store things in eeprom, i don't recommend using it for that reason.
« Last Edit: July 30, 2013, 11:13:10 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Online rsjsouza

  • Super Contributor
  • ***
  • Posts: 3895
  • Country: us
  • Eternally curious
    • Vbe - vídeo blog eletrônico
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #7 on: July 30, 2013, 11:09:45 am »
Although there are many sites around that talk about C, I usually use the one below to solve the typical syntax and stdlib questions.

http://www.cplusplus.com/

In this particular case, searching for struct yields a nice page:

http://www.cplusplus.com/doc/tutorial/structures/
« Last Edit: July 30, 2013, 01:34:54 pm by rsjsouza »
Vbe - vídeo blog eletrônico http://videos.vbeletronico.com

Oh, the "whys" of the datasheets... The information is there not to be an axiomatic truth, but instead each speck of data must be slowly inhaled while carefully performing a deep search inside oneself to find the true metaphysical sense...
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #8 on: July 30, 2013, 11:13:41 am »
Thanks for that.  I'll see how much of the 8K RAM is chewed up once I get more code written.

I can't think of a reason (at the moment) that I'd need to modify any of the button data for this project as the captions and positions will be fixed.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7597
  • Country: nz
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #9 on: July 30, 2013, 11:15:55 am »
Are you sure you have 8K of ram.  Some AVR's do have 8K (ATMega640 for example) but its a pretty high end AVR.

Perhaps you meant 8K of flash?  in which case it probably has 512-1024 bytes ram.
« Last Edit: July 30, 2013, 11:18:08 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #10 on: July 30, 2013, 11:17:53 am »
Although there are many sites around that talk about C, I usually use the one below to solve the typical syntax and stdlib question.

http://www.cplusplus.com/

Thanks for that link.  I'll check it out.

I have programmed in a HLL (Delphi) for PC applications over many years, but the "C way" of doing things is doing my head in sometimes.  I'm not sure if knowing another (different) language is making it easier of harder some days!  lol
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7597
  • Country: nz
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #11 on: July 30, 2013, 11:18:59 am »
I have programmed in a HLL (Delphi) for PC applications over many years, but the "C way" of doing things is doing my head in sometimes.  I'm not sure if knowing another (different) language is making it easier of harder some days!  lol

It's not you, C is just a bad/confusing language.

I also do lots of Delphi programming for windows apps.
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #12 on: July 30, 2013, 11:20:12 am »
Are you sure you have 8K of ram.  Some AVR's do have 8K (ATMega640 for example) but its a pretty high end AVR.

Perhaps you meant 8K of flash?  in which case it probably has 512-1024 bytes ram.

It's an ATxmega128D3-AU, which has 128K of FLASH, 2K of EE and 8K of RAM according to the data sheet.   Cheap as chips (!) too.  :)
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7597
  • Country: nz
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #13 on: July 30, 2013, 11:20:54 am »
ah, its an Xmega.  That would explain it
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #14 on: July 30, 2013, 11:23:52 am »
Yeah, learning C and not using a PIC (like I have for more than 100 other projects) has been a bit of a learning curve.  All while doing the general day to day fun of running a business - sheesh!   ???
 

Online rsjsouza

  • Super Contributor
  • ***
  • Posts: 3895
  • Country: us
  • Eternally curious
    • Vbe - vídeo blog eletrônico
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #15 on: July 30, 2013, 02:37:59 pm »
(...) but the "C way" of doing things is doing my head in sometimes.  (...)
Yes, C is far from trivial and small things such as syntax or prototypes of functions always trip me. In my opinion one of the aspects I appreciate the most in C is the freedom to work with memory (a.k.a. pointers) but, as with everything, with freedom comes great responsibility... :)

One issue that usually trips starters and saves you from a lot of grief in the long run is the bit width of integer data types (int, char, long, etc.), which vary greatly among processors and compiler implementations. For that, always keep in mind that several compilers have an include file called <stdint.h> that contains well-specified types and their bit width. For example: if your code requires an 8-bit unsigned integer variable, instead of declaring it as "unsigned char" you would include the <stdint.h> file on top of our source file (#include <stdint.h>) and declare it as "uint8_t" instead. More details are here and here.

If it helps, you can always use a program called "Lint" to double-check your code. One variant called Splint is freeware and a well-known commercial one (PC-Lint) covers C/C++ and other guidelines.

Yeah, learning C and not using a PIC (like I have for more than 100 other projects) has been a bit of a learning curve.
If anything, I think you are doing it right: starting with a device with beefed up RAM and Flash is always a good idea, since it is one less thing to worry about. 
Vbe - vídeo blog eletrônico http://videos.vbeletronico.com

Oh, the "whys" of the datasheets... The information is there not to be an axiomatic truth, but instead each speck of data must be slowly inhaled while carefully performing a deep search inside oneself to find the true metaphysical sense...
 

Offline grumpydoc

  • Super Contributor
  • ***
  • Posts: 2740
  • Country: gb
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #16 on: July 30, 2013, 02:54:46 pm »
You have typedef'd an annonymous struct. The 'struct' keyword in your array definition isn't necessary; the compiler is looking for a named struct called Button, not a type called Button.

IOW the compiler is expecting:
Code: [Select]
struct Button {
...
};

While the typedef does not create a "struct Button" it does create a "Button" so what is wrong with

Code: [Select]
typedef struct
{
unsigned int top;
unsigned int left;
unsigned int height;
unsigned int width;
char *caption;
} Button;

Button Buttons[4];

 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 9693
  • Country: us
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #17 on: July 30, 2013, 03:51:49 pm »
While the typedef does not create a "struct Button" it does create a "Button" so what is wrong with ...

In standard C, probably nothing. It works on my C compiler.
I'm not an EE--what am I doing here?
 

Offline ToBeFrank

  • Regular Contributor
  • *
  • Posts: 234
  • Country: us
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #18 on: July 30, 2013, 05:34:50 pm »
I needed to add the struct keyword to my draw procedure:

Code: [Select]
void Draw_Button(struct Button Btn);
But it does compile.  I'll be able to test it at work tomorrow.

When you get everything straightened out, you probably want:

Code: [Select]
void Draw_Button(const Button *Btn);
Otherwise, you're passing a copy of the entire struct.
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7805
  • Country: us
  • adieu
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #19 on: July 30, 2013, 05:54:06 pm »
To elaborate on that:

Unlike in C++, which has sneaky behind-the-scenes "references", in C, you pass into the function exactly what you specify. If the function parameter is struct Button Btn, then you are passing the entire struct in, which means copying its data into the function's stack, a big waste of RAM on a MCU.

If you define the parameter as const struct Button *Btn, then you are actually passing in a memory address to the original struct. Instead of passing it foo, pass it &foo, which means "address of foo". And within the function, to access its contents, use Btn->bar instead of Btn.bar, which means "element bar at the address Btn" instead of "element Bar inside Btn".

If you do not declare it const, you will even be able to modify the original struct inside the function without having to return it back out, which may be desirable.
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline Kremmen

  • Super Contributor
  • ***
  • Posts: 1283
  • Country: fi
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #20 on: July 30, 2013, 06:08:12 pm »
Why not just declare/define it a class button{} and be done with it - no pondering what to pass and what not? Or is there a specific reason to avoid the benefits of C++ (seeing how Atmel Studio supports C++ without any additional entry fee).
By the way, something like a button is the archetypical candidate for a C++ class so specifically avoiding making it one is... hmm.
Nothing sings like a kilovolt.
Dr W. Bishop
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7805
  • Country: us
  • adieu
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #21 on: July 30, 2013, 06:10:24 pm »
On one hand, I agree, buttons and other GUI stuff are the perfect application for C++. On the other hand, it's good C practice.
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 9693
  • Country: us
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #22 on: July 30, 2013, 06:16:04 pm »
I don't think I've seen it directly mentioned here, but in C you have to say "struct Button", whereas in C++ you can simply say "Button".

You need to know whether you are using a C compiler or a C++ compiler. There are quite a few differences between C and C++ that can trip you up if you think you are using C++ but you actually have C.
I'm not an EE--what am I doing here?
 

Offline ToBeFrank

  • Regular Contributor
  • *
  • Posts: 234
  • Country: us
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #23 on: July 30, 2013, 06:25:51 pm »
I don't think I've seen it directly mentioned here, but in C you have to say "struct Button", whereas in C++ you can simply say "Button".

That's what the typedef is for.
 

Offline jpb

  • Super Contributor
  • ***
  • Posts: 1711
  • Country: gb
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #24 on: July 30, 2013, 06:30:47 pm »
I always use Button twice :

typedef struct Button
{

}Button;


Button buttons[]

It has always worked with the compilers I've used from Borland to Microsoft and has the advantage I don't have to remember which name is for the struct and which is for the typedef.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf