LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-29-2011, 04:22 PM   #1
thebenjammin
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Rep: Reputation: Disabled
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?

Last edited by thebenjammin; 12-29-2011 at 04:30 PM.
 
Old 12-29-2011, 04:31 PM   #2
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,083

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
"" 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>|"
}

Last edited by johnsfine; 12-29-2011 at 04:32 PM.
 
1 members found this post helpful.
Old 12-29-2011, 05:03 PM   #3
thebenjammin
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
That works. I guess my problem was I wasn't declaring mychar as a char const*. Grrr, get me back to python...
 
Old 12-29-2011, 07:18 PM   #4
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,083

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

Quote:
Originally Posted by thebenjammin View Post
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.

Last edited by johnsfine; 12-29-2011 at 07:19 PM.
 
Old 12-29-2011, 08:01 PM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,522

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by thebenjammin View Post
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*.
 
Old 12-29-2011, 10:32 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,771

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
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;
}

Last edited by NevemTeve; 12-29-2011 at 10:33 PM.
 
  


Reply

Tags
arguments, c++, programing


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Httpd: "Invalid argument: alloc_listener: failed to get a socket for (null)" nirox Slackware 10 07-22-2012 03:13 PM
How can I convert makefile argument "toupper" (and "tolower")? daat99 Programming 4 12-04-2010 10:49 AM
"cp -p" fails with "cp: setting permissions for `/home/svbld/t': Invalid argument" mkhesin Red Hat 1 02-28-2007 12:57 AM
Couldn't display "SMB:///" , No host "(Null)" could been Found?????? munkey Linux - Newbie 3 05-03-2004 04:30 PM
Couldn't display "SMB:///" , No host "(Null)" could been Found?????? help munkey Fedora 6 04-30-2004 06:04 PM


All times are GMT -5. The time now is 07:29 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration