Printing an array of structs?

Having trouble printing members of structs in an array of structs

Page 1 of 1

4 Replies - 7605 Views - Last Post: 28 April 2009 - 02:44 PM Rate Topic: -----

#1 Emper0r  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 07-December 06

Printing an array of structs?

Posted 28 April 2009 - 12:41 PM

Hey guys, so what I'm trying to do is print each member of a struct from every struct in an array. When I try to use the following code, I get an error saying "error request for member 'line' in something not a structure or union. It says this for all members of the structure. Let me know if you guys can see anything wrong:

// Declaring the structure:
typedef struct stEntry{
	char *lexeme;
	int line;
	int idcount;
	} SYMBOLTABLEENTRY;



// Creating the array of structures:
SYMBOLTABLEENTRY *symbolTable[200];



// The function to create a new struct for the array
SYMBOLTABLEENTRY *createEntry(char* yytext, int ln, int id)
{	
	SYMBOLTABLEENTRY *temp_st;
	temp_st = (SYMBOLTABLEENTRY *)malloc(sizeof(SYMBOLTABLEENTRY));
	temp_st->lexeme = strcpy(test, yytext);
	temp_st->line = ln;
	temp_st->idcount = id;
	return temp_st;
}



// When it finds an ID token, it adds a struct to the array
{ID} {printf("found ID token"); symbolTable[tol++] = createEntry(yytext, lineno, idcount++);}



// Where I'm having the problems, trying to print all elements of the struct array:
void printEntries()
   {
		for(i = 0; i < tol; i++)
		{
			sprintf(symbolTable[i].line,"%d:");
			printf("", symbolTable[i].lexeme);
			printf("(", symbolTable[i].idcount, ")\n");
		}
   }



Is This A Good Question/Topic? 0
  • +

Replies To: Printing an array of structs?

#2 Emper0r  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 07-December 06

Re: Printing an array of structs?

Posted 28 April 2009 - 01:08 PM

I posted the code in segments to make it easier to follow, but here's the entire file:

%option noyywrap

%{

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int count = 0;
int lineno = 1;
int idcount = 0;
int tol = 0;
char* test;
int i, j;

typedef struct stEntry{
	char *lexeme;
	int line;
	int idcount;
	} SYMBOLTABLEENTRY;
	
SYMBOLTABLEENTRY *symbolTable[200];

// Create the symbol table entry and return a pointer to that table
SYMBOLTABLEENTRY *createEntry(char* yytext, int ln, int id)
{	
	SYMBOLTABLEENTRY *temp_st;
	temp_st = (SYMBOLTABLEENTRY *)malloc(sizeof(SYMBOLTABLEENTRY));
	temp_st->lexeme = strcpy(test, yytext);
	temp_st->line = ln;
	temp_st->idcount = id;
	return temp_st;
}

// Walk symboltable and print entries.  This is a pre-order depth-first walk
/*
void walkTable( SYMBOLTABLE *ts, int level) {
	/* if tp is not null 
	if (ts) {
		// print the indentation for this node
		int i;
		for (i = 0; i < level; i++) 
			printf( "  ");
		// print the label for this node
		printf( "%s\n", ts->lexeme);
		// process my children
		// both children are at the next level down
		level++;
		walkTable( ts->line, level);
		//walkTable( st->position, level);
	}
	// tp was null, so don't try to do anything.
	return;
}*/

%}

plus "+"
min "-"
times "*"
divide "/"
rbrace "}"
lbrace "{"
lparen "("
rparen ")"
lbrack "["
rbrack "]"
assign "="
eq "=="
noteq "!="
gt ">"
lt "<"
gtoreq ">="
ltoreq "<="
semi ";"
col ":"
com ","
wspc \t
wspcs " "


%x comment
digit [0-9]
NUM {digit}{digit}*
letter [a-zA-Z]
ID {letter}{letter}*
newline \n
if "if"
while "while"
for "for"
else "else"
int "int"
void "void"
return "return"

%%
"/*"         BEGIN(comment);

<comment>[^*\n]*        /* eat anything that's not a '*' */
<comment>"*"+[^*/\n]*   /* eat up '*'s not followed by '/'s */
<comment>\n             printf("%d:\n", lineno++);
<comment>"*"+"/"        BEGIN(INITIAL); 
<comment><<EOF>>		printf("error: EOF in comment."); yyterminate();


{newline} {printf("%d:\n", lineno++); idcount = 0;}
{if} {printf("found IF token.\n");}
{else} {printf("found ELSE token.\n");}
{while} {printf("found WHILE token.\n");}
{for} {printf("found FOR token.\n");}
{void} {printf("found VOID token.\n");}
{int} {printf("found INT token.\n");}
{semi} {printf("found SEMI token.\n");}
{col} {printf("found COL token.\n");}
{assign} {printf("found ASSIGN token.\n");}
{return} {printf("found RETURN token.\n");}


{plus} {printf("found PLUS token.\n");}   
{min} {printf("found MINUS token.\n");}
{times} {printf("found TIMES token.\n");}
{divide} {printf("found OVER token.\n");}
{rbrace} {printf("found RBRACE token.\n");}
{lbrace} {printf("found LBRACE token.\n");}
{rparen} {printf("found RPAREN token.\n");}
{lparen} {printf("found LPAREN token.\n");}
{rbrack} {printf("found RBRACKET token.\n");}
{lbrack} {printf("found LBRACKET token.\n");}
{eq} {printf("found EQUALS token.\n");}
{noteq} {printf("found NEQ token.\n");}
{gt} {printf("found GT token.\n");}
{lt} {printf("found LT token.\n");}
{gtoreq} {printf("found GTEQ token.\n");}
{ltoreq} {printf("found LTEQ token.\n");}
{com} {printf("found COMMA token.\n");}
{NUM} {printf("found NUM token.\n");}
{wspc} {printf("");}
{wspcs} {printf("");}
{ID} {printf("found ID token"); symbolTable[tol++] = createEntry(yytext, lineno, idcount++);}
%%

int main()
{
   printf("%d:\n", lineno++);
   yylex();
   
   void printEntries()
   {
		for(i = 0; i < tol; i++)
		{
			sprintf(symbolTable[i].line,"%d:");
			printf("", symbolTable[i].lexeme);
			printf("(", symbolTable[i].idcount, ")\n");
		}
   }
   
   return 0;
}


Was This Post Helpful? 0
  • +
  • -

#3 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Printing an array of structs?

Posted 28 April 2009 - 01:35 PM

Your sprintf and printf's are incorrect.

The correct way to print out the the structures would be like this:
sprintf(symbolTable[i].line, "%d", /* You need a variable here */);
printf("%s", symbolTable[i].lexeme0);
printf("(%d)\n", symbolTable[i].idcount);


http://www.cplusplus...cstdio/sprintf/
http://www.cplusplus.../cstdio/printf/
Was This Post Helpful? 0
  • +
  • -

#4 Emper0r  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 07-December 06

Re: Printing an array of structs?

Posted 28 April 2009 - 01:57 PM

Alright I got the thing to compile by changing the print function as follows:

void printEntries()
   {
		for(i = 0; i < tol; i++)
		{
			printf("%d", symbolTable[i]->line, ":");
			printf("%s", symbolTable[i]->lexeme);
			printf("( %d", symbolTable[i]->idcount, ")\n");
		}
   }



But now I'm getting the following error when I try to run the .exe:


3 [main] lab3 169968_cygtls::handle_exceptions: Error while dumping state <probably corrupted stack>
Segmentation fault <core dumped>

Anyone know what this means?
Was This Post Helpful? 0
  • +
  • -

#5 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Printing an array of structs?

Posted 28 April 2009 - 02:44 PM

SYMBOLTABLEENTRY *createEntry(char* yytext, int ln, int id)
{	   
		SYMBOLTABLEENTRY *temp_st;
		temp_st = (SYMBOLTABLEENTRY *)malloc(sizeof(SYMBOLTABLEENTRY));
		temp_st->lexeme = strcpy(test, yytext);
		temp_st->line = ln;
		temp_st->idcount = id;
		return temp_st;
}


When you create something in a function and exit the function all of the data is lost. I don't know if their are special cases but, I'm guessing your getting an error because you are trying to access a bad pointer.

Pass a pointer to a structure into the function by reference and modify it that way.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1