Thread: String - Segmentation Fault

  1. #1
    Registered User Raj 89's Avatar
    Join Date
    Nov 2012
    Location
    Chennai, TamilNadu
    Posts
    16

    String - Segmentation Fault

    Hi All,

    I have a problem with this code. I am using a gcc compiler and when i compile and execute this code i am getting a seg fault. I am just assigning two variables, name_1 as pointer and name_2 as string. When i am trying to provide string input for the two values i am getting a seg fault. This seg fault is always associated with the pointer variable that i am using.
    Code:
    #include <stdio.h>
    
    
    int main()
    {
    char *name_1 ;
    char name_2[10] ;
    
    
    /*      Getting 2 strings as an input from the user
            and is stored in the pointer variable name_1 and name_2*/
    scanf("%s",name_1) ;
    scanf("%s",name_2) ;
    
    
    /*      Printing the values of the varibales 
            name_1 and name_2 in string format      */
    printf("\n%s",name_1) ;
    printf("\n%s",name_2) ;
    
    
    printf("\n\n") ;
    return 0 ;
    }
    String - Segmentation Fault-screenshot-2012-11-27-11-33-26-png
    Last edited by Raj 89; 11-27-2012 at 12:26 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You did not allocate any space for the pointer to point to.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Raj 89's Avatar
    Join Date
    Nov 2012
    Location
    Chennai, TamilNadu
    Posts
    16
    Quote Originally Posted by laserlight View Post
    You did not allocate any space for the pointer to point to.
    How i need to allocate a space for it? Can you be more specific?
    Can you make or point out the corrections in my code?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Raj 89
    How i need to allocate a space for it?
    That is up to you. For example, you could create another array instead of just using a pointer, or you could use malloc and free.

    Another mistake is the failure to specify the field width when reading with scanf using %s. It should be something like:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char name_1[10];
        char name_2[10];
    
        scanf("%9s", name_1);
        scanf("%9s", name_2);
    
        printf("\n%s\n%s\n\n", name_1, name2);
    
        return 0;
    }
    Of course, instead of variables named name_1 and name_2 you could have an array of 2 arrays of char instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Raj 89's Avatar
    Join Date
    Nov 2012
    Location
    Chennai, TamilNadu
    Posts
    16
    Quote Originally Posted by laserlight View Post
    Another mistake is the failure to specify the field width when reading with scanf using %s.
    K.. Then i am having a greater doubt. What is the use of providing the field width while using %9s like that.
    As for as in my programs till now i have never used the field width.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Keep the field width and enter "Almu'tasimu Billahi Muhibbuddin Tuanku Alhaj Abdul Halim Mu'adzam Shah Ibni Almarhum Sultan Badlishah, Yang di-Pertuan Agong of Malaysia and Sultan of Kedah" as the name. Remove the field width and enter the same name.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User Raj 89's Avatar
    Join Date
    Nov 2012
    Location
    Chennai, TamilNadu
    Posts
    16
    Sir
    I am just aware of the string width. But i am having another doubt. Consider i am using an array name[20] and i am using scanf to get an input string from the user.
    In this case i am not aware that what is the size of the string that is to be inputted. But i am sure the input will surely be less than 20 bytes. If i am using %10s and i am providing HelloWorldDennis as input i am i will be losing Dennis from the input. In such case what am i supposed to do?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Raj 89
    In this case i am not aware that what is the size of the string that is to be inputted. But i am sure the input will surely be less than 20 bytes. If i am using %10s and i am providing HelloWorldDennis as input i am i will be losing Dennis from the input. In such case what am i supposed to do?
    You should be using %19s instead. 19 characters + null terminator = 20, i.e., the number of elements of the char array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    ... And probably not call laserlight "sir" :P
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String segmentation fault
    By dojha00 in forum C Programming
    Replies: 7
    Last Post: 08-29-2012, 03:30 AM
  2. Too long string causes segmentation fault
    By heinz55 in forum C Programming
    Replies: 11
    Last Post: 08-07-2012, 06:07 PM
  3. Segmentation fault when changing a string
    By lilydjwg in forum C Programming
    Replies: 6
    Last Post: 12-02-2009, 07:43 AM
  4. segmentation fault when processing a string
    By Nakel in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 04:02 PM
  5. segmentation fault when processing a string
    By EMC2 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2003, 02:56 PM