Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


Search this Thread
Old 03-06-2007, 08:07 AM   #1
Registered: Mar 2006
Posts: 536

Rep: Reputation: 42
structure within a structure in C

I've been asked to transfer a bit of code from C++ to C. But am having a small bit of trouble with a .h file. Basically it calls a structure from a structure.

so I have
float a;
float b;
float c;


A_STRUCTURE location; <- problem line
float d;

When I build I get the follwing error

test.h:37: error: syntax error before A_STRUCTURE

I'm sure I could move A_STRUCTURE to another .h file, but other that doing that can I decalre a structure calling a structure in a header file in C? (If that makes sense :s )


Last edited by knobby67; 03-06-2007 at 08:08 AM.
Old 03-06-2007, 08:34 AM   #2
Senior Member
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
In C, you either have to use "struct A_STRUCTURE" whenever you reference that type, or use the typedef idiom:
typedef struct {
   float a;
   float b;
   float c;
My personal preference to use "struct A_STRUCTURE".

Last edited by tuxdev; 03-06-2007 at 08:47 AM.
Old 03-06-2007, 08:34 AM   #3
Senior Member
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie
Posts: 1,445

Rep: Reputation: 83

You either have to add struct before A_ in definiton of B_
struct B_STR{
struct A_str loc;
float b;
or typedef.

Happy Penguins!
Old 03-06-2007, 09:00 AM   #4
Registered: Mar 2006
Posts: 536

Original Poster
Rep: Reputation: 42
Thanks all worked went for struct. All my years programming C and I never knew that. It's nice to learn something so usefull but so easy.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
file_operations structure mp4-10 Programming 6 10-28-2009 02:50 AM
Directory Structure fedix Linux - Newbie 3 10-14-2005 02:48 PM
structure os2 Programming 6 05-29-2005 07:39 PM
Forum Structure Lord-Rashid LQ Suggestions & Feedback 9 05-15-2004 10:31 AM
massage structure? kalleanka Programming 10 02-07-2004 07:17 PM

All times are GMT -5. The time now is 05:13 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration