LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-07-2017, 10:37 AM   #1
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 4,742

Rep: Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877
how to: setting up a struct then giving it a different name


I am using some other code as a guild line to what I am doing. within their code they have a strut.

Code:
struct __my_Struct
{

data name; <-- that part is irrelevant. 

};

extern my_Struct goba;
that is in the header, then in the c file it said again.
Code:
my_Struct goba;
I grep -rwn their entire directory of source code I only find the two instances where it is declared within that same header and C file.

Because it is not actually referencing anything that matches my_Struct, where it should be
Code:
extern __my_Struct goba;
instead. Theirs works, but when I mimic that same way for doing this I get the looked for error.
Code:
options.h:58:8: error: unknown type name 'mhoptions'
 extern mhoptions opts;
        ^
options.c:18:1: error: unknown type name 'mhoptions'
 mhoptions opts;

options.c: In function 'init_system_options':
options.c:22:26: error: 'mhoptions' undeclared (first use in this function)
  memset(&opts, 0, sizeof(mhoptions));
                          ^
If I change everything (mine) and do this with it. Having to remove the two leading underscores,
Code:
struct my_struct
{
data
}oops;

extern my_struct oops;
in the header, then in the C file
Code:
struct my_struct oops;
that way (seems) to work, I say seems, because I just made this change to my file and have not fixed all of the errors it is now throwing due to it no longer being able to see the old data referenced. but going back to the struct

Why is it that they can get this seemingly ill-formed way of declaring and defining a struct to work when I cannot? I say seemingly ill-formed, because it works in theirs.

Last edited by BW-userx; 08-07-2017 at 10:39 AM.
 
Old 08-07-2017, 12:25 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,315

Rep: Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008
You missed this line:
Code:
typedef struct __my_Struct my_Struct;
 
1 members found this post helpful.
Old 08-07-2017, 01:38 PM   #3
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 4,742

Original Poster
Rep: Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877
Quote:
Originally Posted by NevemTeve View Post
You missed this line:
Code:
typedef struct __my_Struct my_Struct;
so adding that typedef redefines it - and ah ha! I just grep looked for __my_Struct this time and not my_Struct and found it. It was hiding in a completely different header redefining it. Just like you said it had to be. tricky little maneuver that was.
 
2 members found this post helpful.
  


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
[SOLVED] Question about Kernel pid_namespace - struct pid and struct upid sreeharsha.t Linux - Kernel 4 03-19-2010 05:41 AM
GCC compile problem:struct A have a member variable which is just a struct type name? leon.zcom Programming 3 04-18-2008 05:40 PM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 11:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 05:30 PM

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

All times are GMT -5. The time now is 12:36 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration