Thread: Printing an array of structs?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    10

    Question Printing an array of structs?

    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:
    Code:
    typedef struct stEntry{
    	char *lexeme;
    	int line;
    	int idcount;
    	} SYMBOLTABLEENTRY;
    // Creating the array of structures:
    Code:
    SYMBOLTABLEENTRY *symbolTable[200];
    // The function to create a new struct for the array
    Code:
    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
    Code:
    {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:
    Code:
    void printEntries()
       {
    		for(i = 0; i < tol; i++)
    		{
    			sprintf(symbolTable[i].line,"%d:");
    			printf("", symbolTable[i].lexeme);
    			printf("(", symbolTable[i].idcount, ")\n");
    		}
       }

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    I posted the code in segments to make it easier to follow, but here's the entire file:

    Code:
    %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;
    }

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    // Where I'm having the problems, trying to print all elements of the struct array:
    Code:
    void printEntries()
       {
    		for(i = 0; i < tol; i++)
    		{
    			sprintf(symbolTable[i].line,"%d:");
    			printf("", symbolTable[i].lexeme);
    			printf("(", symbolTable[i].idcount, ")\n");
    		}
       }
    symbolTable[i] is a pointer to a struct, not a struct by itself. You need to use -> and not the dot operator to access the members.

    Also, this looks questionable:
    Code:
    char* test;
    
    ...
    
    typedef struct stEntry{
    	char *lexeme;
    	int line;
    	int idcount;
    	} SYMBOLTABLEENTRY;
    	
    ...
    
    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;
    }
    Last edited by hk_mp5kpdw; 04-28-2009 at 02:21 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    Alright I got the thing to compile by changing the print function as follows:

    Code:
    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?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your last line is wrong. You make one string, with all of the conversion specifiers in it, and follow it with all the conversion variables.
    Code:
    printf( "( %d )\n", symbolTable[i]->idcount );
    You should start by printing out tol before you start using it in the loop, to see if it's what you expect it to be.

    Also, be more descriptive. What's the last thing it does before it throws a runtime error? Start looking there.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Dynamic Array of Structs help
    By fairguynova in forum C++ Programming
    Replies: 5
    Last Post: 02-04-2009, 11:20 PM
  3. Getting the size of an array of structs
    By steve1_rm in forum C++ Programming
    Replies: 3
    Last Post: 12-17-2008, 06:29 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Pointer to Array of Structs
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-06-2002, 08:34 AM

Tags for this Thread