Parsing Help

This is a discussion on Parsing Help within the C Programming forums, part of the General Programming Boards category; I ran this on two compilers. One gave me a seg fault, the other runs it fine. Code: void parse(char ...

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

    Parsing Help

    I ran this on two compilers. One gave me a seg fault, the other runs it fine.

    Code:
    void parse(char *line)
    {
        char temp;
        int i;
        int j;
        int k, l;
        int length = 0;
        char *string = malloc(sizeof(char) * 50);
        char *firstName  = malloc(sizeof(char) * 50);
        char *lastName  = malloc(sizeof(char) * 50);
        
        do{
            if((*(line + i)>=65)&&(*(line + i)<=90))
            {
                k = 0;
                l= 0;
                j = i;
                while(*(line + j) != ' '){
                    j++;
                }
                j++;
                while(*(line + j) != ' '){
                    j++;
                }
                for(k = i; k < j; k++)
                {
                    *(string + l) = *(line + k);
                    l++;
                }
                //when I'm done it will pass string to another function that will check if it is a name or not, but for now I'm just printing it.
                printf("%s\n", string);
    
            }
            i++;
        }while(i < 100);
    
    }

    Any ideas as to why I get a segfault with one compiler? Or ways I can improve this?

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,384
    Initialise the variable 'i'
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xml parsing in c
    By er.rohan88 in forum C Programming
    Replies: 5
    Last Post: 01-21-2010, 05:39 AM
  2. String parsing(parsing comments out of HTML file)
    By slcjoey in forum C# Programming
    Replies: 0
    Last Post: 07-29-2006, 08:28 PM
  3. Help parsing
    By caduardo21 in forum C Programming
    Replies: 7
    Last Post: 04-02-2005, 11:32 PM
  4. Parsing XML in C++
    By CompiledMonkey in forum C++ Programming
    Replies: 9
    Last Post: 06-22-2004, 11:37 AM
  5. i/o parsing
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-07-2002, 03:21 PM

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