| Author |
Message |
|
|
Posted: Jan 27, 2012 - 09:21 AM |
|


Joined: Jul 18, 2005
Posts: 62371
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
|
Quote:
Its saying it expects a ) before the *.
Can anyone explain this ?
I already did:
Quote:
Let's see that line in context. Will the "Electronic_Serial_Number" type be known to the compiler at the moment it reaches this line?
So did David
Quote:
I would also guess that Electronic_Serial_Number has not yet come into scope.
I also showed a complete example of using struct pointers:
Quote:
Code:
typedef struct {
int n;
char c;
} my_data_type;
void init_data(my_data_type * p) {
p->n = 12345;
p->c = 'A';
}
int main(void) {
my_data_type mydata;
init_data(mydata);
if (mydata.n == 12345) {
UART_send(mydata.c);
}
}
Are you going to bother reading the replies you are given or not?
As for your ESN thing. the only reason you can use:
Code:
#include <stdio.h>
int * n;
int main(void) {
printf("%d", *n);
}
And the compiler doesn't moan about not knowing what "int *" is, is because it's a type already known to the compiler. But if you try:
Code:
#include <stdio.h>
int * n;
mytype * foo;
int main(void) {
printf("%d", *n);
}
You will get:
Code:
error: expected ')' before '*' token
because in this program the compiler has no idea what "mytype" is. I can correct that in one of two ways:
Code:
#include <stdio.h>
typedef struct {
int n;
char c;
} mytype;
int * n;
mytype * foo;
int main(void) {
printf("%d", *n);
}
or
Code:
#include <stdio.h>
#include "contains_type_definition_for_mytype.h"
int * n;
mytype * foo;
int main(void) {
printf("%d", *n);
}
|
_________________
|
| |
|
|
|
|
|
Posted: Apr 19, 2012 - 11:05 PM |
|


Joined: May 08, 2011
Posts: 228
Location: Tooele/Lake Point/Erda, Utah
|
|
|
clawson wrote:
Like Martin I prefer the typedef route:
Code:
typedef struct {
int n;
char c;
} my_data_type;
void init_data(my_data_type * p) {
p->n = 12345;
p->c = 'A';
}
int main(void) {
my_data_type mydata;
init_data(mydata);
if (mydata.n == 12345) {
UART_send(mydata.c);
}
}
The one time you use a struct tag would be for self-reference:
Code:
typedef struct mydata_tag {
int n;
char c;
struct mydata_tag * next;
} my_data_type;
(usually when creating linked lists).
I hate compiler warnings. They are usually a good indication I have done something wrong. When passing a pointer to a structure as in your example I get this:
../GPS_Functions.c:3: warning: 'struct gps_st' declared inside parameter list
../GPS_Functions.c:3: warning: its scope is only this definition or declaration, which is probably not what you want
It gives an error if I take the struct out of the function header.
typedef struct {
int wYear;
. . .
} gps_st;
Function declaration:
void GetLocalTime(gps_st*);
Function Call:
GetLocalTime(&gps_data);
Function:
void GetLocalTime(struct gps_st * foo){
. . .
If I remove "struct", it will not compile. I have tried it with and without the space after *. |
_________________ I use:
AS6 (under duress)
WinAvr 20100110
Windows 7 & XP
STK 600
AVR ONE!(broken and Atmel won't fix)
JTAGICE3 (only works with AS6)
AVRISP II
So far - '88 '2560
Sometimes my own RTOS
|
| |
|
|
|
|
|
Posted: Apr 19, 2012 - 11:32 PM |
|


Joined: Dec 21, 2006
Posts: 1488
Location: Saar-Lor-Lux
|
|
| You definitely have to clean up this mess:
Code:
#include <avr/i2c.c>
#include <avr/macro.c>
#include <avr/spi.c>
#include <avr/MCP23S17.c>
#include <avr/CM404.c>
#include <avr/buzzer.c>
#include <avr/heater_cooler.c>
#include <avr/LEDs.c>
#include <avr/motor.c>
#include <avr/usart.c>
#include <avr/external_EEPROMs.c>
#include <avr/MCP4921.c>
#include <avr/1284P_AD.c>
#include <avr/switches.c>
#include <avr/AVR_setup.c>
#include <avr/DS2401.c>
- Never (as in NEVER!) include C files! Write proper prototypes for global variables and functions, write global macros as needed and typedefs. And put them in headers.
Include these headers in the C files as needed. That way C files can communicate.
- Never (as in NEVER!) put your files as system files. They are no system files. Use #include "foo.h" or perhaps #include "path/foo.h" and tell the root directory of your project include paths to the compiler by means of -I root.
|
|
|
| |
|
|
|
|
|
Posted: Apr 19, 2012 - 11:51 PM |
|


Joined: May 08, 2011
Posts: 228
Location: Tooele/Lake Point/Erda, Utah
|
|
Confused.
My structure is in a header file.
My comment was only about passing the pointer. I get compiler warnings.
Not sure if you caught that I was only saying that if I did the structure pointer passing as described in the quoted passage, it throws errors. I gave small snippets of my code showing that I am passing the pointer in the exact same way. My structure is in fact inside a .H file that is included.... |
_________________ I use:
AS6 (under duress)
WinAvr 20100110
Windows 7 & XP
STK 600
AVR ONE!(broken and Atmel won't fix)
JTAGICE3 (only works with AS6)
AVRISP II
So far - '88 '2560
Sometimes my own RTOS
|
| |
|
|
|
|
|
Posted: Apr 19, 2012 - 11:53 PM |
|


Joined: May 08, 2011
Posts: 228
Location: Tooele/Lake Point/Erda, Utah
|
|
|
SprinterSB wrote:
You definitely have to clean up this mess:
Code:
#include <avr/i2c.c>
#include <avr/macro.c>
#include <avr/spi.c>
#include <avr/MCP23S17.c>
#include <avr/CM404.c>
#include <avr/buzzer.c>
#include <avr/heater_cooler.c>
#include <avr/LEDs.c>
#include <avr/motor.c>
#include <avr/usart.c>
#include <avr/external_EEPROMs.c>
#include <avr/MCP4921.c>
#include <avr/1284P_AD.c>
#include <avr/switches.c>
#include <avr/AVR_setup.c>
#include <avr/DS2401.c>
That code did not come from me. Not sure if you are replying to my message or some other message. |
_________________ I use:
AS6 (under duress)
WinAvr 20100110
Windows 7 & XP
STK 600
AVR ONE!(broken and Atmel won't fix)
JTAGICE3 (only works with AS6)
AVRISP II
So far - '88 '2560
Sometimes my own RTOS
|
| |
|
|
|
|
|
Posted: Apr 20, 2012 - 12:07 AM |
|


Joined: Jul 23, 2001
Posts: 2440
Location: Osnabrueck, Germany
|
|
|
ibapah wrote:
Confused.
That happens if you append your question to an old thread.
Someone might come around and comments on the old original code instead of your new one.
Code:
void GetLocalTime(struct gps_st * foo){
The "struct" here is wrong.
Quote:
If I remove "struct", it will not compile.
And what does "will not compile" mean? Don't you think that the concrete error message might be of use for us? |
_________________ Stefan Ernst
|
| |
|
|
|
|
|
Posted: Apr 20, 2012 - 12:32 AM |
|


Joined: May 08, 2011
Posts: 228
Location: Tooele/Lake Point/Erda, Utah
|
|
| I wish I could. It was complaining of expecting a ( or ; or something. But right after posting that note, the compiler warnings with the "struct" stopped. No idea. I had built all multiple times. Nothing changed that I know about. Just quit throwing warnings. Actually, that is not totally true, I ran the code with the simulator. That is what caused the warnings to stop. I guess if they come back I will make another posting here along with the actual error when the "struct" is removed. |
_________________ I use:
AS6 (under duress)
WinAvr 20100110
Windows 7 & XP
STK 600
AVR ONE!(broken and Atmel won't fix)
JTAGICE3 (only works with AS6)
AVRISP II
So far - '88 '2560
Sometimes my own RTOS
|
| |
|
|
|
|
|
Posted: Apr 20, 2012 - 01:11 AM |
|

Joined: Jun 21, 2005
Posts: 882
Location: Chicago area, USA
|
|
|
SprinterSB wrote:
Never (as in NEVER!) include C files!
As someone who pretty much ALWAYS includes C files, I'll bite. Never? Is it better to put everything into one single file?
(Again, I am not saying that everyone should forget about linking and never learn what a linker is. If someone includes C files just because he/she does not know any better - that is bad and it should be pointed out. But to never include? I say never say never.) |
|
|
| |
|
|
|
|
|
Posted: Apr 20, 2012 - 01:50 AM |
|


Joined: May 08, 2011
Posts: 228
Location: Tooele/Lake Point/Erda, Utah
|
|
I have multiple C files, but I don't use include. I just tell the IDE/project whatever it is called there are multiple source files. I have one (or more) header files with all function declarations and global variable declarations. But the C files don't have to be included.
Not sure the danger in including c files, but with the IDE/project essentially wrapping it all together in a project for you, there is no need.
I think I am missing SprinterSBs main point. |
_________________ I use:
AS6 (under duress)
WinAvr 20100110
Windows 7 & XP
STK 600
AVR ONE!(broken and Atmel won't fix)
JTAGICE3 (only works with AS6)
AVRISP II
So far - '88 '2560
Sometimes my own RTOS
|
| |
|
|
|
|
|
Posted: Apr 20, 2012 - 09:16 AM |
|


Joined: Jul 18, 2005
Posts: 62371
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
|
Quote:
I think I am missing SprinterSBs main point.
Ignore it for now - as Stefan told you your post triggered Georg-Johann to read/reply to the original post - nothing to do with what you were asking. Anyway just to confirm what Stefan said. If I take the example code I posted to this thread:
Code:
typedef struct {
int n;
char c;
} my_data_type;
void init_data(my_data_type * p) {
p->n = 12345;
p->c = 'A';
}
int main(void) {
my_data_type mydata;
init_data(mydata);
if (mydata.n == 12345) {
UART_send(mydata.c);
}
}
and compile that I get:
Code:
avr-gcc -c -mmcu=atmega16 -I. -gdwarf-2 -DF_CPU=8000000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort
-enums -Wall -Wstrict-prototypes --save-temps -fverbose-asm -Wa,-adhlns=./test.lst -std=gnu99 -MMD -MP -MF .dep/test.o.
d test.c -o test.o
test.c: In function 'main':
test.c:14: error: incompatible type for argument 1 of 'init_data'
test.c:16: warning: implicit declaration of function 'UART_send'
The reason for that is that I missed the & as the function invocation was supposed to pass a POINTER to the struct, not the struct itself (we just fell foul of something like this in real code the other day when a 250K struct instead of a pointer to it was being passed and crashing the stack in a DSP RTOS - that was actually because of C++'s pass by reference mechanism which makes it easier to make this error). The code should have read:
Code:
init_data(&mydata);
in which case you only get the warning:
Code:
test.c: In function 'main':
test.c:16: warning: implicit declaration of function 'UART_send'
and that's simply because this is an incomplete test program.
In case you missed it the whole point of typedef'ing a struct and avoiding the use of a tag is for the very reason that you don't then have to sprinkle you code with the word 'struct' all over the place. |
_________________
|
| |
|
|
|
|
|