segmentation fault

This is a discussion on segmentation fault within the C Programming forums, part of the General Programming Boards category; Hi, i am trying to simulate the concatenation of two strings using arrays. I am however running into segmentation fault ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    5

    segmentation fault

    Hi,
    i am trying to simulate the concatenation of two strings using arrays. I am however running into segmentation fault error. I am not sure as to what this means as i am new to C.

    Your help is appreciated.

    Code:
    #include<stdio.h>
    
    /* function prototype */
    void strconcat(char [],char[]);
    main(){
        /* declare variables */
        char first[80];
        char second[80];
        int x = 0, c;
    
        /* Get input */
        printf("Enter first string: ");
        while((c=getchar()) != 10){
            first[x] = c;
            x++;
        }   
        x = 0;
        printf("Enter second string: ");
        while((c=getchar()) != 10){
           second[x] = c;
           x++;
        }
        strconcat(first,second);
        
    }
    
    void strconcat(char f[],char s[]){
        /* declare local variables */
        char c[80];
       
        int i = 0,j = 0;
    
        while(f[i] != 10){
            c[i] = f[i];
            i++;
        }
    
        while(s[j] != 10){
            c[i] = s[j];
            i++;
            j++;
        }   
        printf("The concatenated string is: %s",s);
    }

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157

    Re: segmentation fault

    During your input section, you check if the input character is 10 and never load it into the buffer. Then in the concat routine you expect it to be there.

    Change your input sections to
    Code:
    do
    {
        c=getchar();
        first[x] = c;
        x++;
    } while (c != 10);
    Simply checking after you load the character will correct it.

    Also, use int main() and be sure to return a value.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    5
    WaltP,
    Much thanks to you. That rectified the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 02:36 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, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21