Thread: segmentation error!

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    9

    segmentation error!

    I need some help here.

    basically i have a linked list of strings of the following format.

    "integer operator integer ...="

    it will contain atleast 2 integers but can be of varied length.

    head->expression contains the string. I'm trying to calculate the sum and print it along the string.


    Code:
    void printQueue(struct node *head)
    {
            char *del = " =";
            char* token;
            char* op;
            int sum = 0;
            while(head != NULL)
            {
    
    
                    token = strtok(head->expression,del);
                    while(token!=NULL)
                    {
                            op = strtok(NULL,del);
    
    
                            if (strcmp(op,"+")==0)
                            {
                                    sum = atoi(token) + atoi(strtok(NULL,del));
                            }
                            else if (strcmp(op,"-") == 0)
                            {
                                    sum = atoi(token) - atoi(strtok(NULL,del));
                            }
    
    
    
    
                    }
            printf("%s %d\n", head->expression,sum);
            head = head->next;
            sum = 0;
    }
    }
    I'm getting seg fault or just plain blank output.

    works fine if i just print the strings.
    Last edited by oceans; 07-28-2014 at 07:58 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation error
    By MASX in forum C Programming
    Replies: 6
    Last Post: 03-12-2011, 07:52 AM
  2. Segmentation Error
    By mrdibby in forum C Programming
    Replies: 4
    Last Post: 12-06-2009, 04:25 AM
  3. Segmentation Error / Bus Error in ANSI
    By drag0n69 in forum C Programming
    Replies: 10
    Last Post: 02-05-2008, 09:45 AM
  4. Segmentation error
    By zmaker5 in forum C Programming
    Replies: 5
    Last Post: 08-02-2007, 11:55 AM
  5. What is a segmentation error?
    By Crankit211 in forum C Programming
    Replies: 3
    Last Post: 12-11-2001, 01:08 AM