LinuxQuestions.org
Review your favorite Linux distribution.
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, 10:13 AM   #1
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 352

Rep: Reputation: Disabled
Need help with pointers to structures


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 symbol *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 symbol 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).

I am passing the address of the structure instead of the array index because in the future I will probably malloc the structure symbol and form them into a linked list. My experience with malloc is light so I started with an array of structures. The above is just an example. The real symbol table is much more complex.

Last edited by schmitta; 01-21-2014 at 12:26 PM.
 
Old 01-21-2014, 10:44 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,877
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
There is no such structure as symboltab, the structure you've defined is named symbol
Code:
struct symbol {
	unsigned char 	type;
	char 		*name;
}symbtab[TABLEN];
The following will work:

Code:
struct symbol *i=get_symbtab(test);
printf("%s\r\n",i->name);
Remember that unless you allocate *name for each array element, this will crash. If not all elements of the array will have the string, then recommend you initialize the entire array to zeros, then check to see if the pointer value for name is zero versus not before trying to print out that string. If you do not initialize to zeros or use calloc() then you'll have random (uninitialized) data in there and therefore the pointer value will also be random.

Last edited by rtmistler; 01-21-2014 at 10:45 AM.
 
Old 01-21-2014, 12:33 PM   #3
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 352

Original Poster
Rep: Reputation: Disabled
My bad it should have been struct symbol . Also, I keep track of allocated symbols (the int symbtab_ptr starts at 1 and is incremented for each struct allocated; zero index is for symbol not found) so I only search (for(int index=1;index<symbtab_ptr;index++) ; ) which have all fields defined. I guess my demonstration code should have been more complete. Also I think (*i).name is the same as i->name, but i->name looks better.
 
  


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] array of structures in c, and function pointers. I need a structured approach. karinier Linux - Newbie 1 08-31-2013 01:16 PM
[SOLVED] Freeing pointers to pointers devnull10 Programming 24 07-26-2012 04:58 AM
C structures exvor Programming 8 03-04-2006 12:41 AM
regarding structures eshwar_ind Programming 2 04-25-2005 09:18 AM

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

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