1

I would like to pass in arguments and output them as an array of characters or string. What is the easiest way to do this? Once I have them as an array of characters I want to be able to manipulate the array. I was able to pass them in and use a character pointer to traverse through the string but I'd like to use a char array. Is there any easy way to do this? Im pretty new to C, here is what I have so far.

    if(argc != 3){
            printf("incorrect number of arguments, program needs 3\n");
            exit(1);
    }

    char s1[20];
    s1 = (char*)argv[1];
    char s2[20];
    s2 = (char*)argv[2];

    printf("String1 is %s\n", s1);
    printf("String2 is %s\n", s2);

    exit(0);
1
  • strcpy (argv[1],&s1); or, better: answer by Kerrek SB Commented Feb 6, 2012 at 20:41

4 Answers 4

1

The strings pointed to by the argv array are modifiable, you can just modify the strings pointed to by the argv array, there is no need to copy them.

From the C standard:

(C99, 5.1.2.2.1p2) "The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination."

Sign up to request clarification or add additional context in comments.

6 Comments

Do you have a standard reference for that?
That's not generally a good idea, as changing the length of the argv strings can definitely be a problem.
@KornelKisielewicz yes, same quote in C90 in 5.1.2.2.1
@KerrekSB the quote says the elements of the argv array are modifiable AND the strings are modifiable.
Thank you for the help. How do I access each argument? Can I have example of how I would print character 2 of argument 1?
|
1

You can simply print the values of argv directly:

printf("String1 is %s\n", argv[1]);
printf("String2 is %s\n", argv[2]);

If you need to modify the strings, here is how you would do that

char *newString1 = malloc(strlen(argv[1]) + 1 +  charactersToAdd); // add one for null termination.
strcpy(newString1, argv[1]);

// manipulate newString1 however you choose
printf("String1 is %s\n", newString1);
free(newString1);

2 Comments

He is asking for how to get modifiable variables.
@JohanLundberg ah, I must have misread the OP. I updated my answer now.
1

argv[1] and argv[2] are already pointers to character arrays, so you can use those directly. Moreover (see @ouah's post), the strings are already mutable, so you might not need anything beyond this at all.

If you really want to have a scope-local array, you need to use the C99 feature called "variable-length arrays", since the string lengths are only known at runtime:

#include <string.h>

int main(int argc, char * argv[])
{
    char s1[strlen(argv[1]) + 1];
    strncpy(s1, argv[1], sizeof s1);

    // ...
}

Alternatively you could malloc enough space for a copy of the string (say char * s1 = malloc(strlen(argv[1]) + 1);), but then you wouldn't have a local array after all (and you might as well use the original strings, unless you really wanted to make a copy).

Comments

0

argv is already char* (actually an array of an array), however, they are noted const as in "do not modify" ;). What you want is actually making a copy of them so you can manipulate them freely:

char str[40];
strcpy (str,argv[1]);

Now you can manipulate str as you wish.

3 Comments

But what happens if argv[1]'s length is greater than or equal to 40 characters?
@Richard - sure we can worry about that, and we probably always do. But did you worry when you were just starting out to learn C? It's good to keep things simple in the beginning :).
'Keeping things simple' is all fine and dandy in theory, but perhaps later on this code gets copied into production code and produces a bug that is hard to track down? Personally, I would do strncpy, and pass 39 bytes, and then set the null terminator personally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.