Linked List Error

This is a discussion on Linked List Error within the C Programming forums, part of the General Programming Boards category; Hi, I'm new to C programming and I'm trying to create code ( it's part of a larger program) which ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    7

    Linked List Error

    Hi, I'm new to C programming and I'm trying to create code ( it's part of a larger program) which creates linked list of country codes. Basically I'm getting the country codes inputted from the user which I store in a structure and then I created a separate structure (as requested in assignment question) which will store the country code.

    I'm getting the following error:
    error C2143: syntax error : missing ';' before '.'

    This is part of the code :

    Code:
    void enterclientrecord(const clientrecord_type *clientrecord, listCode** startPtr)
    {
        scanf("%s",clientrecord->countrycode);
    
    
        insertCode(startPtr, clientrecord->countrycode);
            
    };
    
    void insertCode(listCode **startPtr, const char *countryCode)
    {
        listCode *prevNode = NULL, *curNode = *startPtr;
        
        listCode *newNode = (listCode*)malloc(sizeof(listCode));
        
        
            listCode.countryCode = countryCode; //ERROR IS GIVEN HERE
        
        newNode->nextPtr = NULL;
        
        while ((curNode != NULL)&&( strcmp(newNode->countryCode , curNode->countryCode) > 0 ) )
        {
            prevNode = curNode;
            curNode = prevNode->nextPtr;
        };
    
    
        if (prevNode==NULL)
        {
            *startPtr=newNode;
        }    
    
    
        else
        {
            prevNode->nextPtr = newNode;
        };
    
    
        newNode->nextPtr = curNode;
                
    };

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
            listCode.countryCode = countryCode;
    listCode seems to be a struct type
    guess you want
    Code:
            newNode->countryCode = countryCode;
    Kurt

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    I did as you said but now it gave me these errors
    1. warning C4090 =' : different 'const' qualifiers
    2. error C2106: '=' : left operand must be l-value
    I have no idea what they mean?

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Sorry didnt look at the definition of countryCode.
    You will have to use strcpy() to copy the countryCode into the listCode
    Kurt

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also, don't cast malloc(). It will save you some headaches in case you ever forget to include stdlib.h
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    @claudiu .. what is an alternative to malloc?

    Hi, I'm trying to print the items in the list (a continuation from code above) however its not working..

    Code:
    void printList(listCode* startPtr){
    	listCode* curNode = startPtr;
    
    
    	printf("*** Country Stats *** \n");
    
    
    	while (curNode != NULL)
    	{
    		displayStats(curNode);
    		curNode = curNode->nextPtr;
    	};
    };

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I didn't say "don't use malloc" I said "don't cast the return value of malloc".

    i.e.
    Code:
    int *p = (int*)malloc(SOME_SIZE*sizeof(int));
    /* should be */
    int *p = malloc(SOME_SIZE*sizeof(int));
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    ahhh sorry i misunderstood, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list error....
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-09-2009, 08:56 PM
  2. Help with some error...(linked list)
    By google@ in forum C Programming
    Replies: 5
    Last Post: 11-08-2006, 07:29 AM
  3. Linked LIst error
    By Armatura in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2003, 10:40 PM
  4. linked list error
    By breanne in forum C++ Programming
    Replies: 2
    Last Post: 08-13-2002, 12:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21