LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 03-06-2007, 08:07 AM   #1
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Rep: Reputation: 43
structure within a structure in C


Hi,
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
struct A_STRUCTURE
{
float a;
float b;
float c;
};


Then

struct B_STRUCTURE
{
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 )

Thanks

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

Rep: Reputation: 115Reputation: 115
In C, you either have to use "struct A_STRUCTURE" whenever you reference that type, or use the typedef idiom:
Code:
typedef struct {
   float a;
   float b;
   float c;
} A_STRUCTURE;
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
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,832

Rep: Reputation: 108Reputation: 108
Hi,

You either have to add struct before A_ in definiton of B_
Code:
struct B_STR{
struct A_str loc;
/*^^^^*/
float b;
}
or typedef.

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

Original Poster
Rep: Reputation: 43
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.
 
  


Reply


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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:27 AM.

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