CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2004
    Posts
    12

    sscanf question (getting seg fault)

    char* user = NULL;

    sscanf(optarg, "%s", user); //optarg from getopt()

    Anyone see anything wrong with those 2 lines? I'm getting a segfault I think is coming from that line. Thanks.

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    You did not allocate any space for user.

    Code:
    char user[20]; // make sure big enough

  3. #3
    Join Date
    Feb 2004
    Posts
    12
    I guess my understanding of pointers isn't very good.

    For a pointer to char like I have above, when can I point it to a string?

    For example is the following correct:

    char *user = "Tom";

    //later in code I would redefine user
    user = "Ted";

    Thanks.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by Pointextder
    For a pointer to char like I have above, when can I point it to a string?
    In C++, a string has a special meaning -- a std::string.

    What you want is to have an array of char that is big enough to hold all the characters (including the NULL) that you expect to recieve.
    Code:
    char user[500];
    sscanf(optarg, "%s", user);
    For example is the following correct:

    char *user = "Tom";
    It is "correct", but it is incorrect for the purposes that you wanted to use "user" for. The reason why is that
    Code:
    char *user = "Tom";
    is called a string-literal. You cannot change the value of a string-literal (it is undefined behavior to do so), and that is what you would have attempted to do with your call to sscanf().

    Again, you need a modifiable buffer of characters, and give the address of the first character of this modifiable buffer. My example showed an array, but there are other ways to create such a buffer (malloc(), new[], etc.).

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Feb 2004
    Posts
    12
    I'm getting an incompatible types in assignment when I attempt to do the following:

    char username[40] = "default_user";
    ...
    username = optarg; //optarg from call to getopt


    If I declare username as char username*; the assignment will work but I can't change the value of it.

  6. #6
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    You need to use strcpy().

    Code:
    char username[40] = "default_user";
    ...
    
    
    // Must enable the length of optarg is equal or less than username (40 characters including NULL)
    strcpy(username, optarg);

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by Pointextder
    I'm getting an incompatible types in assignment when I attempt to do the following:

    char username[40] = "default_user";
    ...
    username = optarg; //optarg from call to getopt


    If I declare username as char username*; the assignment will work but I can't change the value of it.
    To change an array (which is what username is), you can't do it by using operator =. Arrays are not copyable or assignable (by definition). You need to change each element of the array in a loop, or a function that effectively changes the values using some sort of loop. To do this, there are functions such as strcpy(), memset(), etc. that work with buffers and arrays.

    When you changed username to a pointer, all you did was assign one pointer to another pointer.

    I believe you need to get a good C or C++ tutorial on how to handle NULL-terminated strings and pointers, or if you're using C++, use std::string and not use pointers. If you don't understand the basics of pointers, you will undoubtedly come across more problems.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Aug 2002
    Posts
    78
    > In C++, a string has a special meaning -- a std::string.

    Are you sure? I've been programming in C++ for 10 years now, and when a programmer says "string", they still always mean an array of characters.

    If they mean std:string, then they say std:string.

  9. #9
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Originally posted by Gorgor
    Are you sure? I've been programming in C++ for 10 years now, and when a programmer says "string", they still always mean an array of characters.
    Those are C programmers - C++ programmers mean std::string. Using a char array was an anachronism from the moment when std::string had been included in the C++ standard library.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured