Thread: Need help

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    18

    Need help

    Here is the errors I am getting:

    project7.c: In function `choices':
    project7.c:82: warning: passing arg 2 of `delete' makes pointer from integer wit
    Undefined first referenced
    symbol in file
    isempty /var/tmp/ccRKHdUu.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status

    Here is my code:

    /* counts as line 1 */
    #include <stdio.h>
    #include <stdlib.h>

    struct variable
    {
    char *name;
    void *value;
    int type;
    struct variable *next;
    };


    void linklist(struct variable *, char *);
    void choices(void);
    int empty(struct variable * );
    char delete(struct variable *, char *);

    int main()
    {
    char select;

    choices();
    printf("<prompt>:");
    scanf("%c", &select);
    }

    void choices(void)
    {
    char select ,name[40] ,i,c,f,d,variable;
    struct variable *newptr = NULL;

    printf("\nVariable Allocation in an Operating System");
    printf("\n\n");
    printf("Make your choice from the following choices:");
    printf("\nC to create a new variable");
    printf("\nA to assign a value to a variable");
    printf("\nD to delete a variable");
    printf("\nS to show all variables");
    printf("\nQ to quit the program\n");

    printf("Your choice: ");
    scanf("%c", &select);

    while(( select != 'Q') && (select != 'q'))
    {
    switch( select )
    {
    case 'C' : case 'c' :
    {
    printf(" i Integer\n");
    printf(" c Character\n");
    printf(" f Float\n");
    printf(" d Double\n");
    printf("What type of variable do you choose: \n");
    scanf("%c", &variable);

    if(variable == 'i' || variable == 'c' || variable == 'f' || variable == 'd')
    {
    printf("Name of variable?: ");
    fflush(stdin);
    gets(name);
    }

    linklist(newptr, name );

    break;
    }
    case 'A' : case 'a' :
    {

    printf("%s");
    break;
    }

    case 'D' : case 'd' :

    if(! isempty (newptr) ){
    printf("What variable do you want to delete?: \n");
    scanf("%s", &variable);

    if(! delete (newptr, variable) ){
    printf("Varible %s was deleted\n", variable);

    linklist(newptr, name);
    }
    else
    printf("&s was not found\n", variable);
    }
    else
    printf("List is empty\n");
    break;

    case 'S' : case 's' :

    {
    printf("%s");
    break;

    }

    case 'Q' : case 'q' :

    if (select == 'Q' || select == 'q')
    {
    exit (0);
    }

    default:
    {
    printf("Not a valid choice!\n");

    choices();
    break;
    }

    choices();
    printf("<prompt>:\n");
    scanf("%c", &select);
    }
    }
    }
    void linklist(struct variable *ptr, char *value)
    {

    struct variable *newptr, *prevptr, *current;

    newptr = (struct variable *) malloc(sizeof(struct vaiable *));

    if (newptr != NULL)
    {
    newptr ->name =(char *) malloc(sizeof(strlen(value)+1));
    strcpy(newptr->name, value);
    printf("Address\t Size(bytes)\t Value\t Name\n");

    printf("%x\t%d\t%s\n", newptr ->name, sizeof(newptr->name), newptr->name);

    printf("__________________________________________ _______________\n");

    newptr->next = NULL;
    prevptr = NULL;

    current = ptr;

    while(current != NULL)
    {

    prevptr = current;
    current = current ->next;
    }

    }
    if (prevptr == NULL)
    {
    newptr ->next = ptr;
    ptr = newptr;
    }
    else
    {
    prevptr->next = newptr;
    newptr->next = current;
    }
    }

    int empty (struct variable *ptr)
    {
    return (ptr==NULL ? 1: 0);
    }
    char delete(struct variable *ptr, char *value)
    {
    struct variable *prevptr, *current, *tempptr;

    if (value == (ptr)->value){

    tempptr = ptr;
    ptr = ptr -> next;
    free(tempptr);
    return *value;
    }

    else
    {

    prevptr = ptr;
    current = ptr->next;
    while(current != NULL && current -> value != value)
    {
    prevptr = current;
    current = current -> next;
    }

    if (current != NULL)
    {
    tempptr = current;
    prevptr -> next = current->next;
    free(tempptr);
    return *value;
    }
    }
    return 0;
    }

  2. #2
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    I don't have a lot of time right this second but here are a few things I noticed.
    1. Delete is a keyword ..can't name functions that
    2. Main is returning nothing even though it is decalared as INT.
    3. Several missing ';'


    I may have time to look at it again later today if you haven't solved all the problems yet. Sorry I couldn't be more help at the moment. Have a nice day.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Okay, I finally got to a computer with a compiler and fixed the bugs you had. The main bug that was hiding all of the others was the fact that you used a keyword as a function name. The program was trying to use the C++ keyword delete to free a pointer instead of call your function. Here's the fixed code, I didn't test to see if it worked, that's up to you.
    As a side note, there are several problems in your code that are considered bad style, not the least of which is casting malloc. That can hide errors, which is bad.
    Code:
    /* counts as line 1 */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct variable
    {
    	char *name;
    	void *value;
    	int type;
    	struct variable *next;
    };
    
    
    void linklist(struct variable *, char *);
    void choices(void);
    int empty(struct variable * );
    char del(struct variable *, char *);
    
    int main()
    {
    	char select;
    
    	choices();
    	printf("<prompt>:");
    	scanf("%c", &select);
    	return 0;
    }
    
    void choices(void)
    {
    	char select ,name[40], variable;
    	struct variable *newptr = NULL;
    
    	printf("\nVariable Allocation in an Operating System");
    	printf("\n\n");
    	printf("Make your choice from the following choices:");
    	printf("\nC to create a new variable");
    	printf("\nA to assign a value to a variable");
    	printf("\nD to delete a variable");
    	printf("\nS to show all variables");
    	printf("\nQ to quit the program\n");
    
    	printf("Your choice: ");
    	scanf("%c", &select);
    
    	while(( select != 'Q') && (select != 'q'))
    	{
    		switch( select )
    		{
    			case 'C' : case 'c' :
    				printf(" i Integer\n");
    				printf(" c Character\n");
    				printf(" f Float\n");
    				printf(" d Double\n");
    				printf("What type of variable do you choose: \n");
    				scanf("%c", &variable);
    
    				if(variable == 'i' || variable == 'c' || variable == 'f' || variable == 'd')
    				{
    					printf("Name of variable?: ");
    					fflush(stdin);
    					gets(name);
    				}
    
    				linklist(newptr, name );
    				break;
    			case 'A' : case 'a' :
    				printf("%s");
    				break;
    			case 'D' : case 'd' :
    				if(!newptr ){
    					printf("What variable do you want to delete?: \n");
    					scanf("%s", &variable);
    
    					if(! del (newptr, &variable) ){
    						printf("Varible %s was deleted\n", variable);
    						linklist(newptr, name);
    					}
    					else
    						printf("&s was not found\n", variable);
    				}
    				else
    					printf("List is empty\n");
    				break;
    			case 'S' : case 's' :
    				printf("%s");
    				break;
    			case 'Q' : case 'q' :
    				if (select == 'Q' || select == 'q')
    				{
    					exit (0);
    				}
    
    			default:
    				printf("Not a valid choice!\n");
    				choices();
    				break;
    		}
    		choices();
    		printf("<prompt>:\n");
    		scanf("%c", &select);
    	}
    }
    
    void linklist(struct variable *ptr, char *value)
    {
    	struct variable *newptr, *prevptr, *current;
    
    	newptr = (struct variable *) malloc(sizeof(struct vaiable *));
    
    	if (!empty(newptr))
    	{
    		newptr ->name =(char *) malloc(sizeof(strlen(value)+1));
    		strcpy(newptr->name, value);
    		printf("Address\t Size(bytes)\t Value\t Name\n");
    
    		printf("%x\t%d\t%s\n", newptr ->name, sizeof(newptr->name), newptr->name);
    
    		printf(" ______________________________________\n");
    
    		newptr->next = NULL;
    		prevptr = NULL;
    
    		current = ptr;
    
    		while(current != NULL)
    		{
    			prevptr = current;
    			current = current ->next;
    		}
    
    	}
    	if (prevptr == NULL)
    	{
    		newptr ->next = ptr;
    		ptr = newptr;
    	}
    	else
    	{
    		prevptr->next = newptr;
    		newptr->next = current;
    	}
    }
    
    int empty (struct variable *ptr)
    {
    	return (ptr==NULL ? 1: 0);
    }
    
    char del(struct variable *ptr, char *value)
    {
    	struct variable *prevptr, *current, *tempptr;
    
    	if (value == (ptr)->value){
    		tempptr = ptr;
    		ptr = ptr -> next;
    		free(tempptr);
    		return *value;
    	}
    	else
    	{
    		prevptr = ptr;
    		current = ptr->next;
    		while(current != NULL && current -> value != value)
    		{
    			prevptr = current;
    			current = current -> next;
    		}
    
    		if (current != NULL)
    		{
    			tempptr = current;
    			prevptr -> next = current->next;
    			free(tempptr);
    			return *value;
    		}
    	}
    	return 0;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed