ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am having a massively frustrating time attempting something simple: I have a C++ script, and would like the last argument passed in to be optional. If it is specified, assign it a variable, else have that variable point to "" (or " ", don't care which).
Here's pseudocode of what I'd like to do:
Code:
#include <string.h>
#include <stdio.h>
#include <iostream>
int main(int ac, char* av[]) {
char *a,*b,*c;
a = b = c = 0;
if (ac>1 && strlen(av[1])>0) a=av[1];
if (ac>2 && strlen(av[2])>0) b=av[2];
if (ac>3 && strlen(av[3])>0) c=av[3];
else {
char br = ' ';
c = &br;
}
printf("|%s|%s|%s|\n", a,b,c);
// ./test aa bbb
// |aa|bbb| \252\367_\253\377^?
}
Why is all that garbage showing up?
Last edited by thebenjammin; 12-29-2011 at 04:30 PM.
br is a one byte long temporary object that is out of scope by the time you print c. There is no terminating null following it, so after printing the blank it printed garbage up to the first null after br in memory.
Since br was out of scope, even printing the initial blank would not be reliable. It depends on arbitrary choices that can be made by the compiler.
That works. I guess my problem was I wasn't declaring mychar as a char const*. Grrr, get me back to python...
If you want to develop code in C++, please try to avoid using C.
Consider the following:
Code:
#include <iostream>
#include <string>
int main(int ac, char* av[])
{
std::string a, b, c;
if (ac > 1) a = av[1];
if (ac > 2) b = av[2];
if (ac > 3) c = av[3];
std::cout << "|" << a << "|" << b << "|" << c << "|" << std::endl;
}
If you need to access the "guts" of the std::string, use the c_str() function -- this will return a const char*.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.