Thread: Segmentation faults when trying to modify a character array

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    8

    Unhappy Segmentation faults when trying to modify a character array

    Hello

    I've initialized a character array in my main, and passed it onto one of my functions convertToPostfix(char infix[], char postfix[]). It isn't a constant read only array (constant char*) ive explicitly initialized it to char postfix[] = "";



    Code:
    if ((nextChar > 47) && (nextChar < 58)){    //if its a digit
            postfix[++postfixIndex] = nextChar;
            while (infix[i + 1] > 47 && infix[i + 1] < 58){
                nextChar = infix[++i];
                postfix[++postfixIndex]=nextChar;//segmentation fault when assigning from infix to postfix**
                            }
    please explain what I'm doing wrong....D:

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > It isn't a constant read only array (constant char*) ive explicitly initialized it to char postfix[] = "";
    In which case, it is an array with length 1
    So my guess is you overwrote the end.

    Perhaps
    char postfix[100] = "";
    or some other suitable number for the maximum amount of storage you need.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    8
    i've done that, but nonetheless, I cannot modify it outside of my main, even when explicitly passing it onto another function. and as far as i know, whenever you pass an array down by parameters, it is an automatic call by reference, ergo, why can it not access it?

    i've ended up making a char * in my function to navigate through the indecies and it works. how come it can modify my infix array, but not my postfix? what the heck!?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You should post the whole code, if you want a better diagnosis of a segfault.
    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. Segmentation faults
    By C_Enthuaist in forum C Programming
    Replies: 4
    Last Post: 06-17-2010, 06:47 AM
  2. segmentation faults
    By movie55 in forum C Programming
    Replies: 8
    Last Post: 09-22-2009, 05:31 AM
  3. Segmentation faults out the ****
    By Ubber_C_Noob in forum C Programming
    Replies: 26
    Last Post: 09-11-2005, 10:25 AM
  4. segmentation faults?
    By salsa in forum C Programming
    Replies: 4
    Last Post: 10-08-2004, 09:11 AM
  5. Segmentation faults!
    By Chris Gat in forum C Programming
    Replies: 5
    Last Post: 07-15-2002, 04:54 PM

Tags for this Thread