Thread: More Compiler Errors...

  1. #1
    Registered User P3nGu1N's Avatar
    Join Date
    Feb 2009
    Location
    La Crescent, MN
    Posts
    23

    More Compiler Errors...

    Yes, I'm still having problems coding my SymTab.h file.

    Here is what the compiler is giving me:

    SymTab.h: In function 'EnterName':
    SymTab.h:137: warning assignment makes pointer from integer without a cast
    SymTab.h: At top level:
    SymTab.h:158: error: conflicting types for 'FindName'
    SymTab.h:137: error: previous implicit declaration of 'FindName' was here
    SymTab.h: In function 'EnterName':
    SymTab.h:137: warning: assignment makes pointer from integer without a cast
    SymTab.h: At top level:


    right, so I don't see where there's an integer assignment in this code at all, but maybe somebody can help me see it.

    Here's the code segment that tries to do the assignment, line 137 highlighted for convenience.
    Code:
    bool EnterName(struct SymTab *ATable, const char *Name,
    					struct SymEntry * *AnEntry) {
    	int hashValue = hash(Name,ATable);
    	char n[200];
    	int nameLength;
    	struct SymEntry *currentEntry;
    	struct SymEntry *newEntry;
    	
    	strcpy(n, Name);
    	nameLength = strlen(n) + 1;
    	
    	if(ATable->Contents[hashValue] == NULL) {
    		newEntry = (struct SymEntry*)malloc(sizeof(struct SymEntry*) + nameLength);
    		newEntry->Name = n;
    		newEntry->Next = NULL;
    		ATable->Contents[hashValue] = newEntry;
    		AnEntry = &newEntry;
    		return false;
    	} else {
    		currentEntry = FindName(ATable, Name);[
    		if(currentEntry != NULL) return true;
    		/* else if( currentEntry == NULL )*/
    		currentEntry = ATable->Contents[hashValue];
    		while(currentEntry->Next !=NULL) {
    			currentEntry = currentEntry->Next;
    		}
    		newEntry = (struct SymEntry*)malloc(sizeof(struct SymEntry*) + nameLength);
    		newEntry->Name = n;
    		newEntry->Next = NULL;
    		currentEntry->Next = newEntry;
    	}
    		
    };
    and here's the method I'm trying to use for the assignment, line 158 highlighted.

    Code:
    struct SymEntry* FindName(struct SymTab *ATable,
                               const char * Name) {
    		int comp;
    		int hashValue = hash(Name,ATable);
    		char n[200];
    		int nameLength;
    		struct SymEntry *currentEntry;
    		
    		strcpy(n, Name);
    		nameLength = strlen(n) + 1;
    		
    		if(ATable->Contents[hashValue] == NULL){
    			return NULL;
    		} /*else */
    		for(currentEntry = ATable->Contents[hashValue]; 
    			currentEntry->Next != NULL; currentEntry = currentEntry->Next) {
    			comp = strcmp( n, currentEntry->Name);
    			if (comp == 0){
    				return currentEntry;
    			}
    		}
    		return NULL; /* wasn't found in the list at the hash value */
    };
    I know it's a lot of debugging to ask, but I'm really stumped at this point after going through this code.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You forgot to add a prototype for FindName, so the compiler assumes it's declared as
    int FindName();
    (Note the missing void too.)
    Make a prototype and put it into the header or stuff it at the top and the problem shall be solved.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    why your function bodies are in the header file?

    your FindName function has no prototype before it is called. So compiler supposes that this function is
    int FindName() - returns int - that's why you have your first warning
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User P3nGu1N's Avatar
    Join Date
    Feb 2009
    Location
    La Crescent, MN
    Posts
    23
    do you mean something like this before the method?

    Code:
    struct SymEntry * FindName(struct SymTab *ATable,
                               const char * Name);
    or, what do you mean by 'prototype'?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, that's right.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User P3nGu1N's Avatar
    Join Date
    Feb 2009
    Location
    La Crescent, MN
    Posts
    23
    Alright, put it at the top of my SymTab.h and got a clean compile.

    Okay, so now I'm really confused why my other methods all compiled, but not this one without a prototype.
    Should I have prototypes for all my other methods?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There are two parts with every function: declaration and definition. A prototype is a declaration. A function which contains actual code is a definition, and a definition is also a declaration.
    The compiler must have a declaration of the function you are trying to call before doing the call or it will assume an implicit function declaration (as we stated above).
    So if a function comes before the function that calls it, it works, because the definition of the function is also a declaration, so the compiler knows of it.
    That's why it worked.

    For good practice, it is recommended you use prototypes for each of your functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by P3nGu1N View Post
    Alright, put it at the top of my SymTab.h and got a clean compile.

    Okay, so now I'm really confused why my other methods all compiled, but not this one without a prototype.
    Should I have prototypes for all my other methods?
    You need prototypes for functions that haven't been defined when they are being called - and being VERY technical, you only need it if you have functions that do not return integer. But it is always best to declare a prototype for ALL functions.

    You also should not put function definitions in a header file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User P3nGu1N's Avatar
    Join Date
    Feb 2009
    Location
    La Crescent, MN
    Posts
    23
    Quote Originally Posted by matsp View Post
    You need prototypes for functions that haven't been defined when they are being called - and being VERY technical, you only need it if you have functions that do not return integer. But it is always best to declare a prototype for ALL functions.

    You also should not put function definitions in a header file.

    --
    Mats
    but the .c file is the driver my professor gave me. I assumed he wanted all of the coding done in the .h file.

    Thanks for the explanation, it makes sense that it has to be declared before use.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You PROBABLY should create a second .c file, that contains the implementation of the functions. If you have functions in headers [that are not marked inline or static], then if you use the same header file in two places, you will get "multiple declaration" errors in linking, because you have two functions of the same name - which is not allowed.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User P3nGu1N's Avatar
    Join Date
    Feb 2009
    Location
    La Crescent, MN
    Posts
    23
    Re-read my assignment sheet, and it does seem to want all coding in a separate SymTab.c file. I duplicated my folder, just in case something bad happened when I transferred all the methods over, but it went fairly smoothly.

    I did, however, try to add the following prototypes to SymTab.h after making the big transfer, and it won't compile with them there:

    Code:
    int countEntries(struct SymTab *ATable);
    int hash(char *s, struct SymTab *ATable);
    I know I don't need them, but just out of pure curiosity, why won't it compile with these in my header?

    Compiler Errors:
    SymTab.c: In function 'EnterName':
    SymTab.c:79: warning: passing argument 1 of 'hash' discards qualifiers from pointer target type
    SymTab.c: In function 'FindName':
    SymTab.c:120: warning: passing argument 1 of 'hash' discards qualifiers from pointer target type
    SymTab.c: In function 'countEntries':
    SymTab.c:217: error: number of arguments doesn't match prototype
    SymTab.h:99: error: prototype declaration
    Last edited by P3nGu1N; 03-13-2009 at 10:37 AM.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps hash() should take a const char argument - after all, you aren't changing the string passed in, I guess?

    And the other error is that countEntries takes one argument, and you are not giving it one argumet (you either give it more than one, or zero arguments).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  3. bloodshed compiler errors
    By nerore in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2004, 02:37 PM
  4. How to resolve linking errors?
    By m712 in forum C++ Programming
    Replies: 3
    Last Post: 11-03-2002, 10:17 PM
  5. Compiler errors
    By BellosX in forum C Programming
    Replies: 2
    Last Post: 09-21-2001, 03:24 AM