LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Convert null argument to " " (C++) (https://www.linuxquestions.org/questions/programming-9/convert-null-argument-to-c-921120/)

thebenjammin 12-29-2011 04:22 PM

Convert null argument to " " (C++)
 
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?

johnsfine 12-29-2011 04:31 PM

"" is a char const*

How about

Code:

#include <cstdio>  // to declare printf
int main(int argc, char** argv) {
  char const* mychar;
  if (argc>1) mychar = argv[1];
  else mychar = "";

  printf("mychar:%s|\n",mychar);
  // should print either "mychar: |" if no arg specified, else "mychar:<arg>|"
}


thebenjammin 12-29-2011 05:03 PM

That works. I guess my problem was I wasn't declaring mychar as a char const*. Grrr, get me back to python...

johnsfine 12-29-2011 07:18 PM

It looks like while I was answering, you were editing the original question into an entirely different question.

Quote:

Originally Posted by thebenjammin (Post 4561283)
Code:

                 
        char br = ' ';
        c = &br;
    ...                                   
    printf("|%s|%s|%s|\n", a,b,c);

Why is all that garbage showing up?

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.

dwhitney67 12-29-2011 08:01 PM

Quote:

Originally Posted by thebenjammin (Post 4561310)
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*.

NevemTeve 12-29-2011 10:32 PM

Just to fix the worst parts:

Code:

#include <string.h>
#include <stdio.h>

int main (int argc, char *argv[]) {
    char *a= "", *b= "", *c= "";

    if (argc>1) a=argv[1];
    if (argc>2) b=argv[2];
    if (argc>3) c=argv[3];

    printf("|%s|%s|%s|\n", a,b,c);

    return 0;
}



All times are GMT -5. The time now is 09:32 AM.