Thread: Quick question about SIGSEGV

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    23
    Code:
    int main (void)
    {
            char* s;
            char* p = "hello";
            printf("Programme to reverse all elements in a string, using recursion.");
            printf("\nYou will be required to enter a word up to 20 letters long.\n");
                                                                                                                    
            printf("WORD: HELLO");/*
            p = strin(stdin);*/
                                                                                                                    
            printf("\n %s", p); <==== SIGSEGV
                                                                                                                    
            s = reverse(p);
                                                                                                                    
            puts(s);
            return 0;
    }
    I won't post the rest of the code, namely the reverse function, because that part isn't even reached when seg fault occurs. The part of the programme we're talking about is just a normal assignment and output.
    If pointers have made you suicidal, you're not alone. But there is no need to hurt yourself. There are people who are willing to help. Just call your local C Crisis Centre and talk to the professtionals there. If you don't have a C3 in your neighborhood, go to the global C3 at www.cprogramming.com . If you're still suicidal, don't lose hope. Gently close your book on C and throw it in the fireplace. If you live in a tower, you can throw it out the window. There, doesn't that feel better?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The problem is your reverse function. You're trying to modify a string literal. It seems like the problem is the print statement, but because you don't have an fflush after the printf or a newline at the end of the format string, the buffer isn't flushed and you don't see the output before reverse is called. Change your print to this and see what happens:
    Code:
    printf("\n %s\n", p);
    Then change your declaration to this and see the problem vanish:
    Code:
    char p[] = "hello";
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM