LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 12-19-2004, 07:03 AM   #1
xconspirisist
Member
 
Registered: Dec 2002
Location: United Kingdom
Distribution: Ubuntu
Posts: 276

Rep: Reputation: 30
Corrupted struct?


Using C, writing it in Linux, Compiling with GCC. I'm having a wierd problem with structs. The structs are defined in public.h, data is written to them in main.c, but comes out very odd when read in public.c.

output

Code:
debug level: 134515272
debug level: 0.000000
port: -18390300
debug level: 2532503
debug level: 0.000000
port: 2866320
debug level: 2532503
debug level: 0.000000
port: 2866320
debug level: 2532503
debug level: 0.000000
port: 2866320
inc/public.h

Code:
*** 

struct options_t {
	int debug;
};

struct arguments_t
{
  char *args[2];            
  int port;            
};

***
src/main.c

Code:
int main (int argc, char **argv)
{	
	struct arguments_t s_arguments;
	struct options_t s_options;

	// set defaults
	s_arguments.port = 0;
	s_options.debug = 1;
	
	printf("%i\n", s_options.debug);
	
	debug("Parsing command line arguments...\n");
			
	argp_parse (&argp, argc, argv, 0, 0, &s_arguments);
	
	return 0;
}
src/public.c

Code:
void error ( const char *error_message ) {

	printf("\nERROR: %s \n\n", error_message );
	exit(0);
	
}

void debug ( const char *debug_message ) {
	struct options_t s_options;
	struct arguments_t s_arguments;
		
	printf("debug level: %i\n", s_options.debug);
	printf("debug level: %f\n", s_options.debug);
	printf("port: %i\n", s_arguments.port);
	
	if (s_options.debug == 1)
		printf("\nDEBUG: %s\n", debug_message );

}
Any help is greatly appreciated. Many thanks.
 
Old 12-19-2004, 07:47 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
As far as I can tell, you're not doing what you intend to do.

In the debug() function, you declare new structures, but never initialize them to anything. So that's why junk gets printed. The structures you modified in main() are not global, and you don't pass them to your function.

The structure definitions themselves (in public.h) are global, but when delaring a variable of that type, they exist only within a specific scope (even if the variables have the same names as other declarations).
 
Old 12-19-2004, 07:59 AM   #3
xconspirisist
Member
 
Registered: Dec 2002
Location: United Kingdom
Distribution: Ubuntu
Posts: 276

Original Poster
Rep: Reputation: 30
Many thanks for the help. I fixed it in the end

Last edited by xconspirisist; 12-19-2004 at 08:53 AM.
 
Old 12-19-2004, 08:58 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Here's the thing, global variables are bad. That is, general guidelines for "good" programming practice discourage creating global variables unless there is no other alternative. The more accepted method is to pass variables by value or by reference. And that just means adding one (or more) arguments to your functions depending on how you want to use those arguments inside the function.

It's been a long time since I looked for tutorials, but I just went to google and plugged "C tutorial variable scope" in and got a lot of hits. One of them was the GNU C Programming Tutorial. It looks promising, but I can't vouch for it
 
  


Reply



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
operator== for struct lordofring Programming 10 11-21-2005 12:09 PM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
using struct type X as pointer in struct X. worldmagic Programming 1 10-28-2003 02:06 PM
Accessing a struct inside struct cxel91a Programming 1 09-17-2003 04:24 PM

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

All times are GMT -5. The time now is 11:38 PM.

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