Thread: Segmentation fault - C on a Unix m/c

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    3

    Segmentation fault - C on a Unix m/c

    Hi i have written this code, there is a problem with it !!!

    void stcp(char *newstr,char *oldstr,char *slash)
    {
    int counter1 = 0;
    int counter2 = 0;
    int counter3 = 0;

    while ((oldstr[counter1] != NULL) && (counter1 < MAX_FILE_NAME))
    {
    if (oldstr[counter1] != 012)
    {
    newstr[counter2++] = oldstr[counter1];
    slash[counter3++] = oldstr[counter1];
    if (newstr[counter2 - 1] == 057)
    counter3 = 0;
    }
    counter1++;
    }
    newstr[counter2] = NULL;
    slash[counter3] = NULL;
    }

    When i compile the program i get a warning like this

    test.c: In function `stcp':
    test.c:134: warning: comparison between pointer and integer
    test.c:145: warning: assignment makes integer from pointer without a cast
    test.c:146: warning: assignment makes integer from pointer without a cast
    test.c: In function `main':
    test.c:798: warning: return type of `main' is not `int'

    Line 134 while ((oldstr[counter1] != NULL) && (counter1 < MAX_FILE_NAME))
    Line 145 newstr[counter2] = NULL;
    Line 146 slash[counter3] = NULL;

    Why does testing for and assiging NULL give me problems - how can i resovle this ???????

    also the last error, i have defined my main function to return Void, do i actually have to return void in the function ???


    PLEASE HELP !!

  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
    > Why does testing for and assiging NULL give me problems - how can i resovle this
    By understanding the difference between the NULL pointer ( which is (void*)0 ), and the nul character ( which is '\0' )

    In this code, you want

    oldstr[counter1] != '\0'
    etc


    > also the last error, i have defined my main function to return Void
    guess again - main returns an int
    As in
    Code:
    int main ( ) {
        // your code
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    3
    Thanks very much, thats a great help

    I take that means the assigment should change from

    newstr[counter2] = NULL;

    to

    newstr[counter2] = '\0' ;

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    3
    I forgot to ask, so you cant have

    void main ( ) {
    // your code
    }

    Does main have to return something ??

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM