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.
Printable View
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.
You did not allocate any space for user.
Code:char user[20]; // make sure big enough
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.
In C++, a string has a special meaning -- a std::string.Quote:
Originally posted by Pointextder
For a pointer to char like I have above, when can I point it to a 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);
It is "correct", but it is incorrect for the purposes that you wanted to use "user" for. The reason why is thatQuote:
For example is the following correct:
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().Code:char *user = "Tom";
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
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.
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);
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.Quote:
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.
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
> 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.
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.Quote:
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.