Getting errors in my code

This is a discussion on Getting errors in my code within the C Programming forums, part of the General Programming Boards category; Code: #include <stdlib.h> #include <string.h> struct CHlst /*define element of list*/ { char c; CHlst* next; } void main() { ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    15

    Getting errors in my code

    Code:
    #include <stdlib.h>
    #include <string.h>
    struct CHlst /*define element of list*/
    {
        char c;
        CHlst* next;
    }
    void main()
    {
        char str[200]; /*string*/
        int count=strlen(str); /*number of chars*/
        CHlst* begin=NULL, *ptr=NULL; /*pointer to begin and pointer to current char*/
        printf("Enter string: ");
        scanf("%s",str); /*input*/
        if(count) /*if string isn`t empty*/
        {
            begin = (CHlst*)malloc(sizeof(CHlst)); /*give memory*/
            if(!begin) /*if error*/
            {
                printf("Not enough memory\n");
                return;
            }
            begin->c=str[0]; /*write char from string*/
            begin->next=NULL; /*pointer to next char NULL*/
            ptr=begin; /*begin is current char*/
        }
        for(int i=1;i<count;++i) /*for each char*/
        {
            ptr->next=(CHlst*)malloc(sizeof(CHlst)); /*give memory*/
            if(!ptr) /*if error*/
            {
                printf("Not enough memory\n");
                ptr=begin; /*current to begin*/
                CHlst* tmp; /*temp variable*/
                do /*clear memory*/
                {
                    tmp=ptr->next; /pointer to next character
                    free(ptr);/*delete memory*/
                    ptr=tmp; /*new current*/
                }while(tmp!=NULL); /*until list will be empty*/
                return; /*exit*/
            }
            ptr=ptr->next; /*to next char*/
            ptr->c=str[i]; /*write it*/
            ptr->next=NULL; /*pointer to next char NULL*/
        }
        ptr=begin; /*to begin*/
        while(ptr) /*throw all list*/
        {
            printf("%c",ptr->c); /*print chars*/
            ptr=ptr->next;
        }
        printf("\n");
        ptr=begin; /*to begin*/
        while(ptr)
        {
            if((ptr==begin) && (ptr->c=='a' || ptr->c=='e' || ptr->c=='i' || ptr->c=='o' || ptr->c=='u' || ptr->c=='y')) /*if we have a vowels in the begining*/
            {
                begin=ptr->next; /*new begin*/
                free(ptr); /*clear memory*/
                ptr=begin; /*new current*/
            }
            else
                break;
        }
        if(!begin) /*if there is no more chars*/
        {
            printf("There is no more chars\n");
            return;
        }
        ptr=begin; /*to begin*/
        while(ptr->next)
        {
            if(ptr->next->c=='a' || ptr->next->c=='e' || ptr->next->c=='i' || ptr->next->c=='o' || ptr->next->c=='u' || ptr->next->c=='y')
            { /*if found vowels in the middle of string in the next element*/
                CHlst* tmp=ptr->next; /*write pointer to next element of list*/
                ptr->next=tmp->next; /*current element will point to next element of tmp*/
                free(tmp); /*free memory*/
            }
            else
                ptr=ptr->next; /*to next element*/
        }
        ptr=begin; /*to begin*/
        while(ptr)
        {
            printf("%c",ptr->c); /*print every element*/
            ptr=ptr->next;
        }
        printf("\n");
        ptr=begin;
        CHlst* tmp;
        do
        { /*clear all list*/
            tmp=ptr->next;
            free(ptr);
            ptr=tmp;
        }while(tmp!=NULL);
    }
    Error list

    Compiling: C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.cC:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:6:2: error: unknown type name 'CHlst'
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:8:1: error: expected ';', identifier or '(' before 'void'
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:8:6: error: return type of 'main' is not 'int' [-Wmain]
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c: In function 'main':
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:12:2: error: unknown type name 'CHlst'
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:13:2: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:13:2: warning: incompatible implicit declaration of built-in function 'printf' [enabled by default]
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:14:2: warning: implicit declaration of function 'scanf' [-Wimplicit-function-declaration]
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:14:2: warning: incompatible implicit declaration of built-in function 'scanf' [enabled by default]
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:17:12: error: 'CHlst' undeclared (first use in this function)
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:17:12: note: each undeclared identifier is reported only once for each function it appears in
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:17:18: error: expected expression before ')' token
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:23:8: error: request for member 'c' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:24:8: error: request for member 'next' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:27:2: error: 'for' loop initial declarations are only allowed in C99 mode
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:27:2: note: use option -std=c99 or -std=gnu99 to compile your code
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:29:6: error: request for member 'next' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:29:20: error: expected expression before ')' token
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:34:11: error: 'tmp' undeclared (first use in this function)
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:37:12: error: request for member 'next' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:37:20: error: expected expression before '/' token
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:43:10: error: request for member 'next' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:44:6: error: request for member 'c' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:45:6: error: request for member 'next' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:50:18: error: request for member 'c' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:51:10: error: request for member 'next' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:57:26: error: request for member 'c' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:57:41: error: request for member 'c' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:57:56: error: request for member 'c' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:57:71: error: request for member 'c' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:57:86: error: request for member 'c' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:57:101: error: request for member 'c' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:59:13: error: request for member 'next' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:72:11: error: request for member 'next' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:74:9: error: request for member 'next' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:74:30: error: request for member 'next' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:74:51: error: request for member 'next' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:74:72: error: request for member 'next' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:74:93: error: request for member 'next' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:74:114: error: request for member 'next' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:76:18: error: request for member 'next' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:77:7: error: request for member 'next' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:81:11: error: request for member 'next' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:86:18: error: request for member 'c' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:87:10: error: request for member 'next' in something not a structure or union
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:94:10: error: request for member 'next' in something not a structure or union
    Process terminated with status 1 (0 minutes, 0 seconds)
    41 errors, 4 warnings
    Last edited by Hasnat Abul; 11-01-2012 at 06:25 PM.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,367
    Declare main as below - Making sure to put "return 0;" at the end
    Code:
    int main
    {
      ...
      return 0;
    }



    All returns with error need to be "return 1;"
    Code:
    if(!begin) /*if error*/
    {
      printf("Not enough memory\n");
      return 1;
    }



    Struct definition
    Code:
    struct CHlst /*define element of list*/
    {
        char c;
        struct CHlst* next;
    };

    Struct use for the malloc - Note that you could use a typedef if you wanted
    Code:
    struct CHlst* begin = NULL... /*pointer to begin and pointer to current char*/
    (struct CHlst*)malloc(sizeof(struct CHlst)); /*give memory*/
    struct CHlst* tmp; /*temp variable*/
    struct CHlst* tmp=ptr->next; /*write pointer to next element of list*/



    You forgot to include
    Code:
    #include <stdio.h>

    Take definition out of for loop
    Code:
    int i;
    for(i=1; i<count; ++i)

    Error with comment
    Code:
    /pointer to next character
    And then you will get it compiling without errors and warnings
    Last edited by Click_here; 11-01-2012 at 06:40 PM. Reason: Add colour
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    15
    Thank you a lot. I fixed most of the errors. However, there are still few more left. I have mentioned the errors below.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    struct CHlst /*define element of list*/
    {
        char c;
        struct CHlst* next;
    };
    
    int main()
    {
        int i;
        char str[200]; /*string*/
        int count=strlen(str); /*number of chars*/
        struct CHlst* begin=NULL, *ptr=NULL; /*pointer to begin and pointer to current char*/
        printf("Enter string: ");
        scanf("%s",str); /*input*/
        if(count) /*if string isn`t empty*/
        {
            begin = (struct CHlst*)malloc(sizeof(struct CHlst)); /*give memory*/
            if(!begin) /*if error*/
            {
                printf("Not enough memory\n");
                return 1;
            }
            begin->c=str[0]; /*write char from string*/
            begin->next=NULL; /*pointer to next char NULL*/
            ptr=begin; /*begin is current char*/
        }
    
    
        for(i=1; i<count; ++i) /*for each char*/
        {
            ptr->next=(struct CHlst*)malloc(sizeof(struct CHlst)); /*give memory*/
            if(!ptr) /*if error*/
            {
                printf("Not enough memory\n");
                ptr=begin; /*current to begin*/
                struct CHlst* tmp; /*temp variable*/
                do /*clear memory*/
                {
                    tmp=ptr->next; /*pointer to next character*/
                    free(ptr);/*delete memory*/
                    ptr=tmp; /*new current*/
                }while(tmp!=NULL); /*until list will be empty*/
                return 0; /*exit*/
            }
            ptr=ptr->next; /*to next char*/
            ptr->c=str[i]; /*write it*/
            ptr->next=NULL; /*pointer to next char NULL*/
        }
        ptr=begin; /*to begin*/
        while(ptr) /*throw all list*/
        {
            printf("%c",ptr->c); /*print chars*/
            ptr=ptr->next;
        }
        printf("\n");
        ptr=begin; /*to begin*/
        while(ptr)
        {
            if((ptr==begin) && (ptr->c=='a' || ptr->c=='e' || ptr->c=='i' || ptr->c=='o' || ptr->c=='u' || ptr->c=='y')) /*if we have a vowels in the begining*/
            {
                begin=ptr->next; /*new begin*/
                free(ptr); /*clear memory*/
                ptr=begin; /*new current*/
            }
            else
                break;
        }
        if(!begin) /*if there is no more chars, that is error*/
        {
            printf("There is no more chars\n");
            return 1;
        }
        ptr=begin; /*to begin*/
        while(ptr->next)
        {
            if(ptr->next->c=='a' || ptr->next->c=='e' || ptr->next->c=='i' || ptr->next->c=='o' || ptr->next->c=='u' || ptr->next->c=='y')
            { /*if found vowels in the middle of string in the next element*/
                struct CHlst* tmp=ptr->next; /*write pointer to next element of list*/
                ptr->next=tmp->next; /*current element will point to next element of tmp*/
                free(tmp); /*free memory*/
            }
            else
                ptr=ptr->next; /*to next element*/
        }
        ptr=begin; /*to begin*/
        while(ptr)
        {
            printf("%c",ptr->c); /*print every element*/
            ptr=ptr->next;
        }
        printf("\n");
        ptr=begin;
        struct CHlst* tmp;
        do /*clear all list*/
        {
            tmp=ptr->next;
            free(ptr);
            ptr=tmp;
        }while(tmp!=NULL);
    }
    ERRORS
    Compiling: C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c: In function 'main':
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:38:4: error: ISO C90 forbids mixed declarations and code [-pedantic]
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:95:2: error: ISO C90 forbids mixed declarations and code [-pedantic]
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:102:1: warning: control reaches end of non-void function [-Wreturn-type]
    Process terminated with status 1 (0 minutes, 0 seconds)
    2 errors, 1 warnings

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,367
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c: In function 'main':
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:38:4: error: ISO C90 forbids mixed declarations and code [-pedantic]
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:95:2: error: ISO C90 forbids mixed declarations and code [-pedantic]
    You need to declare your variables at the top of each section for Pre-C99
    C:\Users\Hasnat\Desktop\New folder\Assignment 1 Embed C\new.c:102:1: warning: control reaches end of non-void function [-Wreturn-type]
    You forgot to put "return 0;" at the end of main
    A quick example of what I mean by declaring variables:
    Code:
    int main(void)
    {
      int k;
    
      k = 3;
    
      int i;
    
      i = 4;
    
      return 0;
    
    }
    This would need to change to
    Code:
    int main(void)
    {
      int k;
    
      int i;
    
      k = 3;
    
      i = 4;
    
      return 0;
    
    }
    Just move them up to the previous curly brace
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    15
    Okay thanks. That fixed it.
    How can I refrain from using strlen and free functions, I am trying to do it in C. IN other words, i am trying to replace them.
    Also how do I separate functions and loops outside main and call them from main instead of cluttering it to look neat?

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    15
    This is the final code, but it does not work after compilation.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    struct charList /*struct list of char elements*/
    {
    	char c;
    	struct charList* next;
    };
    
    
    int main() /*Main function*/
    {
        int i;
        char strings[200]; /*Declare array containing upto 200 strings*/
    	int count=strlen(strings); /*Check length of the string and store it*/
    	struct charList* begin=NULL, *ptr=NULL; /*pointer to begin and pointer to current char*/
    	struct charList* tmp;
    	printf("Enter string: ");
    	scanf("%s",strings); /*Get input from user*/
    	if(count) /*Check if string isn`t empty*/
    	{
    		begin = (struct charList*)malloc(sizeof(struct charList)); /*Allocate memory*/
    		if(!begin) /*if there is an error*/
    		{
    			printf("Not enough memory\n");
    			return 1;
    		}
    		begin->c=strings[0]; /*write char from string*/
    		begin->next=NULL; /*pointer to the next char NULL*/
    		ptr=begin; /*begin is current char*/
    	}
    
    
    	for(i=1; i<count; ++i) /*for each char*/
    	{
    		ptr->next=(struct charList*)malloc(sizeof(struct charList)); /*Allocate memory*/
    		ptr=begin; /*set current char to begin*/
    		if(!ptr) /*if there is not enough memory, clear memory*/
    		{
    			struct charList* tmp; /*temp variable*/
    			printf("Not enough memory\n");
    			ptr=begin; /*set current char to begin*/
    			do /*clear memory*/
    			{
    				tmp=ptr->next; /*pointer to next character*/
    				free(ptr);/*delete memory*/
    				ptr=tmp; /*new current*/
    			}while(tmp!=NULL); /*until list will be empty*/
    			return 0; /*exit*/
    		}
    		ptr=ptr->next; /*to next char*/
    		ptr->c=strings[i]; /*write it*/
    		ptr->next=NULL; /*pointer to next char NULL*/
    	}
    
    
    
    
    	ptr=begin; /*to begin*/
    	while(ptr) /*throw all list*/
    	{
    		printf("%c",ptr->c); /*print chars*/
    		ptr=ptr->next;
    	}
    	printf("\n");
    
    
    
    
    	ptr=begin; /*to begin*/
    	while(ptr)
    	{
    		if((ptr==begin) && (ptr->c=='a' || ptr->c=='e' || ptr->c=='i' || ptr->c=='o' || ptr->c=='u' || ptr->c=='y')) /*check for vowels in the beginning*/
    		{
    			begin=ptr->next; /*new begin*/
    			free(ptr); /*clear memory*/
    			ptr=begin; /*new current*/
    		}
    		else
    			break;
    	}
    
    
    
    
    	if(!begin) /*error check if there are no more chars*/
    	{
    		printf("There are no more chars\n");
    		return 1;
    	}
    
    
    
    
    	ptr=begin; /*to begin*/
    	while(ptr->next)
    	{
    		if(ptr->next->c=='a' || ptr->next->c=='e' || ptr->next->c=='i' || ptr->next->c=='o' || ptr->next->c=='u' || ptr->next->c=='y') /*check for vowels in the middle of string in the next element*/
    		{
    			struct charList* tmp=ptr->next; /*write pointer to next element of list*/
    			ptr->next=tmp->next; /*current element will point to next element of tmp*/
    			free(tmp); /*free memory*/
    		}
    		else
    			ptr=ptr->next; /*to next element*/
    	}
    
    
    
    
    	ptr=begin; /*to begin*/
    	while(ptr)
    	{
    		printf("%c",ptr->c); /*print every element*/
    		ptr=ptr->next;
    	}
    	printf(" \n");
    
    
    
    
    	ptr=begin;
    	do /*clear all list*/
    	{
    		tmp=ptr->next;
    		free(ptr);
    		ptr=tmp;
    	}while(tmp!=NULL);
    
    
    
    
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    15
    The objective is to use a linked list to remove vowels from a string.

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Hasnat Abul View Post
    This is the final code, but it does not work after compilation.
    So it's not the final code, is it :-)

    Code:
    int main() /*Main function*/
    {
        int i;
        char strings[200]; /*Declare array containing upto 200 strings*/
        int count=strlen(strings); /*Check length of the string and store it*/
    "strings" will contain garbage, thus using strlen() on it will return a random value (depending where it finds the first '\0' in the garbage).
    You should call strlen() after you get the user input.

    Code:
    for(i=1; i<count; ++i) /*for each char*/
    {
        ptr->next=(struct charList*)malloc(sizeof(struct charList)); /*Allocate memory*/
        ptr=begin; /*set current char to begin*/
    You don't cast the return value of malloc() in C.
    You reset "ptr" to "begin" on each iteration, thus you overwrite the letter from the iteration before. Just remove this line.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with errors in the code
    By lukis123 in forum C Programming
    Replies: 5
    Last Post: 11-03-2011, 11:00 PM
  2. Help with errors on my code!
    By ashleyd in forum C++ Programming
    Replies: 7
    Last Post: 10-30-2011, 01:35 PM
  3. Replies: 2
    Last Post: 05-09-2010, 11:30 PM
  4. 2 errors in my code
    By johnnyg in forum C++ Programming
    Replies: 22
    Last Post: 05-04-2006, 08:04 AM
  5. Errors (not in code)
    By Da-Spit in forum C++ Programming
    Replies: 8
    Last Post: 05-18-2002, 12:31 AM

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