LinuxQuestions.org
Help answer threads with 0 replies.
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 01-21-2014, 07:44 AM   #16
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 352

Original Poster
Rep: Reputation: Disabled

I have the following :
Code:
#define TABLEN 1000
struct symbol {
	unsigned char 	type;
	char 		*name;
}symbtab[TABLEN];

char token[80]={"alpha1"};
// .
// .
// .
struct symbol *get_symbtab(char *);
int main()
{
char * test;
// .
// .
// .
test=token;
struct symboltab *i=get_symbtab(test);
printf("%s\r\n",(*i).name);		// line 20 -- will this line work? Will (*i).name
					// be known as the name member of the 
					// structure symbol?
// .
// .
// .
}

struct symbol *				// returns the address of the structure symbol.
get_symbtab(				// routine returns address of sysbol table start
					// for the array structure containing the address
					// of the symbol *temp.
	char *temp			// the address of the string to look for in 
					// the symbol table.
	)
{
//
	int index;
// .
// .
// .
for (index=0;index<TABLEN;index++)	  // index trrough the complete symbol table
	if((strcmp(temp,symbtab[index].name))==0)//symbol found?
		return(&symbtab[0]+index);// returns the address of the structure containing the 
					  // string *temp located in the symbol table.

}
Will the code on line marked 20 work in that the returned address will address the proper structure of the array symbtab and that the C compiler will know that I am talking about the struct symbol pointed to by i? (ie. (*i).name?) right now it is causing a out-of-bounds memory access (fault).

Last edited by schmitta; 01-21-2014 at 10:08 AM.
 
Old 01-21-2014, 08:23 AM   #17
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by schmitta View Post
Will the code on line marked 20 work in that the returned address will address the proper structure of the array symbtab and that the C compiler will know that I am talking about the struct symbol pointed to by i? (ie. (*i).name?) right now it is causing a out-of-bounds memory access (fault).
An advisable thing to do here is to create a new thread, for a couple of reasons.
  1. Exposure of the question portion related to C syntax to those who may contribute but not have much to offer because they think it's about GDB
  2. Highlight of a C syntax question for others searching for similar questions
I would personally not bother returning the pointer to a structure; just return the index since the array and size of it is global. I would make that strcmp() be a strncmp() and use the size of the strings contained in your structure.

One problem is that your structure is not allocated.
Code:
struct symbol {
    unsigned char type;
    char *name;
}symtab[TABLEN];
name is a pointer. You need to either allocate and initialize that structure or start by declaring those strings with fixed sizes; like:
Code:
char name[80]
The problem occurs when you're printing out the string "name" you are printing out the contents of an uninitialized pointer.

Another recommendation is to enable as much compiler warnings as you can. Here's a selection of warning flags which I typically use, because -Wall does not catch them all.

Code:
-Werror -Wall -Wextra -Wswitch-default -Wswitch-enum -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wmissing-noreturn
Don't ignore warnings, here's the output if you just compile with no extra flags:
Code:
~/testcode$ gcc -o table table.c
table.c: In function ‘main’:
table.c:19: warning: initialization from incompatible pointer type
table.c:20: warning: incompatible implicit declaration of built-in function ‘printf’
table.c:20: error: dereferencing pointer to incomplete type
Here's the output if you put in extra warning flags:
Code:
~/testcode$ gcc -Werror -Wall -Wextra -Wswitch-default -Wswitch-enum -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wmissing-noreturn -o table table.c
cc1: warnings being treated as errors
table.c:13: error: function declaration isn’t a prototype
table.c: In function ‘main’:
table.c:19: error: initialization from incompatible pointer type
table.c:20: error: implicit declaration of function ‘printf’
table.c:20: error: incompatible implicit declaration of built-in function ‘printf’
table.c:20: error: dereferencing pointer to incomplete type
table.c: In function ‘get_symbtab’:
table.c:42: error: implicit declaration of function ‘strcmp’
 
Old 01-21-2014, 10:08 AM   #18
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 352

Original Poster
Rep: Reputation: Disabled
The reason I am returning a pointer is that in the future I may not have an array but use malloc to get the structure which would be made into a linked list. My experience with malloc is light so I started with arrays.
 
Old 01-21-2014, 10:14 AM   #19
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
That's fine to return a pointer. You haven't allocated *name, that's this problem.
 
  


Reply

Tags
error, gcc, system, ubuntu



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
error in compiling SDL code using gcc richman1234 Programming 3 02-01-2013 06:56 PM
gcc throws Segmentation fault runtime error mrajdeep Programming 9 08-22-2011 02:30 PM
Trying to see assembly code on Runtime Azrai Programming 1 07-18-2006 11:44 PM
Strange GCC compile error with C code exvor Programming 16 08-23-2005 06:05 PM
error in catch and try . code is attached ashwinipahuja Programming 6 07-05-2004 10:13 PM

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

All times are GMT -5. The time now is 08:30 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