How to turn argv[1] into a string in C?


 
Thread Tools Search this Thread
Top Forums Programming How to turn argv[1] into a string in C?
# 1  
Old 02-11-2011
Error How to turn argv[1] into a string in C?

i have a function that's parameter is char *s

and in the main function i am sending that function &(argv[1]), but i dont think this is working, how can i fix this?

can i cast it to be a string or something?

is there a way i can create a new string thats exactly what argv[1] is equal to without specifying how long it is?
# 2  
Old 02-12-2011
The parameter you have to use is simply argv[1], not &(argv[1]) .

argv[1] is just a pointer, and so it can be a char* .
# 3  
Old 02-12-2011
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    char *str = (char *)NULL;
    int len = 0;

    if (argc != 2) return 1;

    len = strlen(argv[1]);
    printf("Length of argv[1] = %d\n", len);

    if ((str = malloc(len + 1)) != NULL) {
       bzero(str, len + 1);
       strncpy(str, argv[1], len);
       printf("argv[1] = %s\n", str);
    }

    free(str);

    return 0;
}

# 4  
Old 02-12-2011
A few things to consider.

Quote:
Originally Posted by fpmurphy
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    char *str = (char *)NULL;
    int len = 0;

    if (argc != 2) return 1;

    len = strlen(argv[1]);
    printf("Length of argv[1] = %d\n", len);

    if ((str = malloc(len + 1)) != NULL) {
       bzero(str, len + 1); /* Does the example warrant zeroing out the memory?; bzero is a deprecated function, use memset instead */
       strncpy(str, argv[1], len);
       printf("argv[1] = %s\n", str);
    } else {  
           exit(EXIT_FAILURE);   /* if not, it will try to free memory not allocated */
    }

    free(str);

    return 0;
}

# 5  
Old 02-12-2011
Quote:
Code:
  if ((str = malloc(len + 1)) != NULL) {
       bzero(str, len + 1); /* Does the example warrant zeroing out the memory?; bzero is a deprecated function, use memset instead */
       strncpy(str, argv[1], len);
       printf("argv[1] = %s\n", str);
    } else {  
           exit(EXIT_FAILURE);   /* if not, it will try to free memory not allocated */
    }

The comment regarding trying to free memory is patently incorrect as I specifically handled that case with
Code:
char *str = (char *)NULL;

The long starting practice since the dawn of the C language is that if the argument to free() is a null pointer, no action shall occur. This was and is codified by all versions of the various C standards.

As regards the comment that bzero() is deprecated, yes it is deprecated on some platforms and in some standards and specifications but is not depreciated in others. I did not claim any particular conformance so the comment lacks standing.

Last edited by fpmurphy; 02-13-2011 at 12:25 PM..
# 6  
Old 02-13-2011
Quote:
Originally Posted by fpmurphy
The comment regarding trying to free memory is patently incorrect as I specifically handled that case with
Code:
char *str = (char *)NULL;

Patently? Perhaps, if I understand you correctly. Incorrect? Too absolute for the case. Rather redundant, and most likely unnecessary like your use of bzero() in this case. Or the casting of the NULL for the same matter.

Of course, as you, I must make emphasis of what I did not "claim": I did not claim to be correct, nor did I wrote any comment or addition as a vehicle for your personal engagement.
At the risk of stating the obvious, at the top of the quote I wrote. A few things to consider. You take it or leave it, writing generally, that is.


Quote:
Originally Posted by fpmurphy
,
As regards the comment that bzero() is depreciated, yes it is deprecated on some platforms and in some standards and specifications but is not depreciated in others. I did not claim any particular conformance so the comment lacks standing.
While I never said "depreciated", and the emphasis was more in the question mark, the stated "lacking of standing", I leave it unchallenged for others to judge and make their own opinion, if any.

As for more information, to whoever has interest, bzero() is part of the Berkeley UNIX C library, and memset() is part of the AT&T UNIX C library. The C Standard Library favored memset() and adopted it, and bzero() became deprecated. The use of the latest, is discouraged in favor of portability.
# 7  
Old 02-13-2011
Code:
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    char *str;
    if((argc>=2)&&(str=strdup(argv[1])))
    {
            printf("argv[1] = %s\n", str);
            free(str);
            return 0;
    }
    else
    {
        return 1;
    }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

ARGV how to use it?

So i am trying to read in file readFile <GivenFile> modFile looking for a regular file under the directories in the GivenFile and print them out is my over all goal. basically I am looking for anything that looks like a directory in the given file and printing it out. Since I am trying to do... (2 Replies)
Discussion started by: squidGreen
2 Replies

2. UNIX for Advanced & Expert Users

O argv, argv, wherefore art thou argv?

All of my machines (various open source derivatives on x86 and amd64) store argv above the stack (at a higher memory address). I am curious to learn if any systems store argv below the stack (at a lower memory address). I am particularly interested in proprietary Unices, such as Solaris, HP-UX,... (9 Replies)
Discussion started by: alister
9 Replies

3. Programming

help with C, argv

when i run my program, i have a parameter, that i want to set the value to another string i am using int main(int argc, char **argv) { char my_str=argv; printf("%s",my_str); return 0; } and i get Segmentation fault ran using ./my_prog /usr/share/dict/words hello1 ... (2 Replies)
Discussion started by: omega666
2 Replies

4. Programming

ARGV help in C

Hi, Can somehelp help how to list file in a dir? (5 Replies)
Discussion started by: Learnerabc
5 Replies

5. Programming

Building an argc/argv style structure from a string (char*)

Hello All, First post. I've been struggling with the following: Given a char* string, I need to construct an "int argc, char *argv" style structure. What I'm struggling with most is handling escaped-whitespace and quotes. e.g. the string: char *s = "hello world 'my name is simon'... (10 Replies)
Discussion started by: cbarwise
10 Replies

6. Shell Programming and Scripting

$#Argv in Csh

Hello all, Had a quick question: In a typical csh script should inputting via stdin (i.e. set i = $< ) increase the value of $#argv ? echo enter an value: set val= "$<" if($#argv == 0) then echo No args else echo The arg is $argv so if a value is inputted #argv... (1 Reply)
Discussion started by: new2C
1 Replies

7. Shell Programming and Scripting

if #argv = (this OR that) then...

this is in one of my scripts... if ($#argv == 0) then echo 'blah bla' exit 0 endif I want it to be something like this... if ($#argv == 0 OR $argv >=3) echo 'blah bla' exit 0 endif so when the arguments are none, or greater than three I want this "if then" to take over. how? I... (5 Replies)
Discussion started by: ajp7701
5 Replies

8. Programming

help for argv argc

Hi C experts, I have the following code for adding command line option for a program int main (argc, argv) int argc; char *argv; { char *mem_type; //memory type char *name; //name of the memory int addr; //address bits int data; ... (5 Replies)
Discussion started by: return_user
5 Replies

9. Programming

Using argv argc

I searched on the forums. No advises. I am using a previous source code. I changed the main function main(int argc, char **argv) in a function misc(int argc, char **argv). How do you use the argc and argv parameters? This is how I am calling the function : char param; strcat(param,"wgrib ");... (4 Replies)
Discussion started by: Akeson Chihiro
4 Replies

10. Programming

argv

I have a program which I wish to modify. It used to be run from the command line, but now I wish to change this so it can be used as a function. The program has complex argument processing so I want to pass my paramters to as if it were being called by the OS as a program. I have tried to... (2 Replies)
Discussion started by: mbb
2 Replies
Login or Register to Ask a Question