Thread: expected primary-expression before "struct"

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    3

    expected primary-expression before "struct"

    Hi boys, I wrote this nice program in c++ a good deal was copied dev-c++ latest version keeps giving this error at the compilation : expected primary-expression before "struct"

    I write here just the first part of the code till the mistake, you will find it at the line before the last.
    I need it for a working interview and I am a bit nervous about it.
    Thanks in advance-
    Dora



    Code:
                                   
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    //#include "sort_words.h"
    struct word_item {
    	char * word;
    	struct word_item * next;
    };
    
    struct word_list {
    	int length;
    	struct word_item * first_word;
    	struct word_list * next;
    };
    
    struct word_list * first_word_list = NULL;
    
    char * read_word(FILE * fin){
    	char c;
    	int i;
    	char * buffer = (char *)malloc(sizeof(char));
    	buffer[0] = '\0';
    	for (i=0; (c = fgetc(fin))!=EOF; i++) {
    		if (isspace(c) || ispunct(c))
    			break;
    		buffer[i] = c;
    		buffer = (char *)realloc(buffer, sizeof(char)*(i+1));
    		buffer[i+1] = '\0';
    	}
    	if (c == EOF)
    		return NULL;
    	return buffer;
    }
    
    /*
    void init_word_list() {
    	first_word_list = NULL;
    }
    */
    
    struct word_list * 
    init_word_list_item(int length, struct word_list * next, struct word_item * item) {
    	struct word_list * word_list_item;
    	word_list_item = (struct word_list *) malloc(sizeof(struct word_list));
    	if (word_list_item == NULL) {
    		printf("cannot allocate enough memory\n");
    		exit(-1);
    	}
    	word_list_item->length = length;
    	word_list_item->next = next;
    	word_list_item->first_word = item;
    	return word_list_item;
    }
    
    /*void append_word_item(struct word_list * current, struct word_list * new) {
    	new->next = current->next;
    	current->next = new;
    }*/
    
    struct word_list * get_word_list_item(int length) {
    	struct word_list * cwl = NULL;
    	struct word_list * new = NULL;       // Here the error is given
    	struct word_list * previous = NULL;

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You're probably compiling the code as C++. new is an operator in C++, so you can't have an identifier with that name.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    3
    yes I am compiling it as c++ that is the idea.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Why? That's not C++ code at all. It looks like C code to me, so you should compile it as C code.

    But if you insist on compiling it as C++, then stop using a variable called "new": change it to something else and the compiler should be happy.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    3
    Thinking better you are right. So now my problem is to compile it in c under windows.

    I never used c so i did not even know how it looked like. Dev-c++ makes any distinction between the 2?

    Thanks.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes. Just save the file as a ".c" file, and Dev-C++ will automatically compile it as C code.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Grey Wizard C_Sparky's Avatar
    Join Date
    Sep 2009
    Posts
    50
    yeah you write this as if it's C, you don't need to have "struct" before you declare a new object of that structure.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Doraemon View Post
    I never used c so i did not even know how it looked like. Dev-c++ makes any distinction between the 2?
    It seems to me that you've always been using C!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    19
    Quote Originally Posted by iMalc View Post
    It seems to me that you've always been using C!
    I agree. I don't see much c++ in there. For example, "malloc", "realloc", "fgetc", "printf" are C-operators.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Walle View Post
    I agree. I don't see much c++ in there. For example, "malloc", "realloc", "fgetc", "printf" are C-operators.
    Those are C library functions - which are vastly different things from operators.

    The original code will never compile as C++, as "new" is a reserved keyword in C++, so cannot be used as a name of a variable.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    Jan 2010
    Posts
    19
    Oh ofc, I was being sloppy. The point is that they are C, not C++, though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. expected primary expression before "." token
    By melodious in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2007, 04:39 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM