0

I'm new to C, and I'm trying to make a string of variable length, like this:

int main(int argc, char *argv[]) {
    if (argc > 1) {
        char filename[] = argv[1];
    }
    else {
        char filename[] = "temp.txt";
    }
    printf("%s", filename);
}

Of course, that doesn't work because the scope of the string is only within the conditional statement.

How can I assign this variable to a string of an unknown length based on conditional statements?

2
  • If the string is read-only, then you can assign the literal string to char* Commented Sep 27, 2012 at 15:52
  • You can only do that with a char*, arrays of different lengths have different types. Commented Sep 27, 2012 at 15:52

5 Answers 5

4

This is one way:

char* filename = "this is a different one";

if (boolean)
    filename = "this is a file name";

printf("%s", filename);
Sign up to request clarification or add additional context in comments.

10 Comments

Even better: char *str = condition? "one string": "another";
Thanks! Could you explain why initializing it as a char pointer works, but initializing it as an array of chars doesn't?
@larsmans yep, you can do that too if you want maximum shortness.
@CypressFrankenfeld you just need filename to be in the same scope as the printf for it to work. Also, you cannot assign to arrays but you can initialise them; char filename[] = "asdf"; works b/c you are initialising the array with elements, but not char filename[] = argv[1] because you are trying to assign a pointer to an array which doesn't work. The way I wrote it works because arrays decay to pointers to their first element, which you can store and use later.
@CypressFrankenfeld use strncpy if you'll potentially be modifying your filename later in your code, i.e. adding more path components using strcat. In that case, you shouldn't simply assign a pointer to an element of argv
|
2

In simple cases, as in your example, you can declare a char * at an outer scope:

char *str = "a different file name";
if (boolean)
    str = "this file name";

But anything more complicated and you'll need to keep a buffer. Typically filename/path manipulation is more complicated. You can do the same as above with e.g. argv[1], but instead I'd copy the argument, because I might be changing filename at some point later in my program, and just assigning a new pointer to the argument array, or to a literal string, would cause problems.

char filename[MAX_PATH + 1];

strncpy(filename, "temp.txt", MAX_PATH);
if (argc > 1)
    strncpy(filename, argv[1], MAX_PATH);

// .. later
strncat(filename, "temp.txt", MAX_PATH);

But if you're only setting filename once, the following will also work:

char *filename = "temp.txt";

if (argc > 1)
    filename = argv[1];

2 Comments

Can you briefly explain the MAX_PATH part? I'm not quite sure how you're determining the length of the filename array.
@CypressFrankenfeld sure, if you're going to use a buffer for this, you might begin by assuming an upper-bound on the number of chars in filenames that you'll use. declaring the string with MAX_PATH makes space for strings of a certain size, and is an actual constant for this in windows code. outside windows, it's more complicated, see: this answer. you could also alloc a buffer to hold exactly the number of chars in argv[1], but you couldn't append later without allocating more space
1

You have to declare the filename variable outside of the if statement, otherwise it won't be visible after the if statement. Inside the if statement you have to use strcpy to copy the string into the variable. If you don't have to be able to change the string you can use a char pointer instead of an char array.

Comments

0
char filename[256];

if (boolean) {
    strcpy(filename,"this file name");
}
else {
    strcpy(filename, "a different file name");
}
printf("%s", filename);

Or you can use malloc to allocate memory and copy the string.

1 Comment

While correct, this is much too complicated. It requires keeping track of how long the string literals are and allocating the appropriate amount of memory for them. The advice to use malloc makes things even worse.
0

Define the string before your conditional statement. Then use a string copy function, such as strcpy, to place the value you want into the string. For example:

char filename[ MAX_PATH ];  
if (boolean) { 
    strcpy( filename, "this file name" ); 
} 
else { 
    strcpy( filename, "a different file name" ); 
} 
printf("%s", filename);

Comments

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.