Thread: syntax linked list problem & struct problem

  1. #1
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291

    Unhappy syntax linked list problem & struct problem

    well, i should have to pass up my assignment soon, but there's a lot of syntx error that could found. could you help me about those syntax ?? thanks for help ...
    Code:
    ================= struct pointer ==================
    
    void add (NODE *ptr, int v1, int v2);
    
    #include <stdio.h>
    
    typedef struct
    {
    	int a;
    	int b;
    	int ans;
    } NODE;
    
    int main ()
    {
    	NODE start;
    	int num1, num2;
    
    	printf ("enter 2 num > ");
    	scanf ("%d %d", num1 ,num2);
    
    	add (&start, num1, num2);
    
    	return 0;
    }
    
    void add (NODE *ptr, int v1, int v2)
    {
    	ptr->a = v1;
    	ptr->b = v2;
    	ptr->ans = v1 + v2;
    
    	printf("the ans is %d\n\n", ptr->ans);
    }
    
    
    
    =============linked list ====================
    
    #include <stdio.h>
    #include <stdlib.h>
    
    void printvalue (NODEPTR *sptr);
    void insertvalue (NODEPTR *sptr, char value);
    
    struct nodelist{
    	char data;
    	struct nodelist *nextptr;
    };
    
    typedef struct nodelist NODE;
    typedef NODE *NODEPTR;
    
    int main ()
    {
    	// local definition
    	NODEPTR start = NULL;
    	char value;
    	char again;
    
    	do {
    		printf ("Please enter the value (char) > ");
    		scanf ("%c", &value);
    
    		insertnode (&start, value);
    		printnode (&start);
    
    		printf ("\nInsert again ? > ");
    		scanf ("%c", &again);
    
    	} while (toupper (again) == 'Y')
    
    	return 0;
    }
    
    void insertvalue (NODE *sptr, char value)
    {
    	NODEPTR newptr;
    	NODEPTR cptr;
    	NODEPTR pptr;
    
    	newptr = (NODE *)malloc (sizeof (NODE));
    	if (!newptr) {
    		printf ("ERROR ... no memory available!")
    		exit (100);
    	}
    	else {
    		newptr->data = value;
    		newptr->nextptr = NULL;
    
    		cptr = *sptr;
    
    		while (value >= cptr->data && cptr != NULL)
    		{
    			pptr = cptr;
    			cptr = cptr->nextptr;
    		}
    		pptr->nextptr = newptr;
    		newptr->nextptr = cptr;
    	}
    }
    
    void printvalue (NODE *sptr)
    {
    	NODEPTR cptr;
    
    	printf ("\n\nThe result is...\n\n");
    	for (cptr = *sptr ; cptr != NULL ; cptr = cptr->nextptr)
    	{
    		printf ("%d",cptr->data);
    		printf (" -> ");
    	}
    	
    	printf ("NULL");
    }
    Code tags added by Hammer

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    First up, use code tags when posting code. See my signature for an example and some help on this matter.


    Now the errors are these, but if you've taken notice of what your compiler says, you already know these:
    Code:
    Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
    junk1.c:
    Error E2147 junk1.c 2: 'NODE' cannot start a parameter declaration
    Warning W8075 junk1.c 21: Suspicious pointer conversion in function main
    Error E2356 junk1.c 27: Type mismatch in redeclaration of 'add'
    Error E2344 junk1.c 2: Earlier declaration of 'add'
    Error E2040 junk1.c 37: Declaration terminated incorrectly
    Error E2147 junk1.c 42: 'NODEPTR' cannot start a parameter declaration
    Error E2147 junk1.c 43: 'NODEPTR' cannot start a parameter declaration
    Error E2238 junk1.c 50: Multiple declaration for 'NODE'
    Error E2344 junk1.c 11: Earlier declaration of 'NODE'
    Error E2238 junk1.c 54: Multiple declaration for 'main'
    Error E2344 junk1.c 13: Earlier declaration of 'main'
    Warning W8065 junk1.c 64: Call to function 'insertnode' with no prototype in function main
    Warning W8065 junk1.c 65: Call to function 'printnode' with no prototype in function main
    Warning W8065 junk1.c 70: Call to function 'toupper' with no prototype in function main
    Error E2378 junk1.c 72: Do-while statement missing ; in function main
    Warning W8070 junk1.c 73: Function should return a value in function main
    Error E2356 junk1.c 76: Type mismatch in redeclaration of 'insertvalue'
    Error E2344 junk1.c 43: Earlier declaration of 'insertvalue'
    Error E2379 junk1.c 84: Statement missing ; in function insertvalue
    Error E2096 junk1.c 90: Illegal structure operation in function insertvalue
    Warning W8057 junk1.c 100: Parameter 'sptr' is never used in function insertvalue
    Error E2356 junk1.c 103: Type mismatch in redeclaration of 'printvalue'
    Error E2344 junk1.c 42: Earlier declaration of 'printvalue'
    Error E2096 junk1.c 107: Illegal structure operation in function printvalue
    Warning W8057 junk1.c 114: Parameter 'sptr' is never used in function printvalue
    *** 18 errors in Compile ***
    Try fixing them yourself, and ask specific questions when you are stuck.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    well, is the struct pointer problem actually? i tried other example from another book. but the error shown as same as the above's error.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    I compiled your code with GCC, since you didn't specify which compiler you give, there may be differences between your and my results.

    In struct pointer:

    1. Put the prototype of add after de definition of NODE, currently the compiler doesn't know the type NODE when it comes to the prototype and will give an error.

    2. When using scanf to read the numbers, you must add the & before the variable-names, since scanf expects the address of the variables.

    In linked list:

    1. Also here the prototypes can use the definitions of your datatypes only if those definitions are known.

    2. The types used in the prototype must be the same as in the definition of the function. Look at how you prototyped your function insertvalue and how you defined it.

    3. When you allocate memory, using malloc for example, you must always explicitly free this memory using the function free!

    4. Each statement must be ended with ;.

    5. The function insertnode should be insertvalue.

    6. Using NODEPTR is enough in the functions, NODEPTR* is not required, I'd propose you prototype your functions as:

    void printvalue (NODEPTR sptr);
    void insertvalue (NODEPTR sptr, char value);

    7. You could use more descriptive names to make reading easier. Like in your function insertvalue you could do this like:

    Code:
    list_ptr = root_ptr;
    
    while (value >= list_ptr->data && list_ptr != NULL)
    {
          prev_node = list_ptr;
          list_ptr = list_ptr->nextptr;
    }
    8. The insertion is not correct. You should make distinction between the situation if list_ptr equals NULL or if it does not equal NULL.

    Code:
    /* If list_ptr equals NULL, the value is larger or equal than any current data in the list, so the new node must be put on the end of the list, else the new node must be inserted. */
    if (list_ptr == NULL)
    {
        prev_node->nextptr = new_node;
    }
    else
    {
        new_node->nextptr = list_ptr;
        prev_node->nextptr = new_node;
    }
    [edit]
    Maybe I have forgotten some things, but I hope these points help you a little further.
    [/edit]
    Last edited by Shiro; 11-10-2002 at 09:30 AM.

  5. #5
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    i got the answer actually,
    the function prototype definition should be placed after the typdeef struct definition.... heehee .... i use Visual studio to compile my C prog

  6. #6
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    oops, that's correct for joking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with linked list and shared memory
    By Sirfabius in forum C Programming
    Replies: 10
    Last Post: 11-10-2008, 04:45 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM