Thread: Standish Chapter 7 (C/Unix)

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    9
    Hello,

    I just wanted o followup on this program. After much trial and error, the reason I was getting the seg fault was the example problem had the space allocated in the PostfixString.c file as:


    Code:
    char *s = "x";
    I changed it to:


    Code:
    char s[]="x";
    % gcc -Wall -o myProgram PostfixString.c StackImplementation.c -lm
    % ./myProgram
    Value of postfix expression = 20.000000


    I just wanted to thank Salem, kermit and dwks from this post. This was really a great example problem with unexpected twists! :-)

    I really want to learn to program and will continue to try more programs. Any advice on how to become a better programmer?

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > char *s = "x";
    > I changed it to:
    > char s[]="x";
    Whilst this fixes the problem because you now allocate writable memory (the segfault happens because something tried to modify read-only memory), I have to ask - why is the string being changed at all?

    > Any advice on how to become a better programmer?
    Years and years of daily practice.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In need of guidance
    By ajdspud in forum C++ Programming
    Replies: 7
    Last Post: 06-01-2016, 02:23 AM
  2. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM