This is a very simple problem, but for some reason I can't get it to work. I need to empty a variable ( char fileName[MAX_PATH] =""; ).

It needs to be to where when I use this that it sees if it equals "", this wont work if it equals NULL will it?:

if (fileName = "")
{
}

How could I do this? I have tried:

fileName = "";
fileName = NULL;
fileName[MAX_PATH] = "";

Those all return errors, why can't I empty the variable?

Recommended Answers

Use memset...

memset(fileName, 0, MAX_PATH);

Rewrites the elements to 0.

Jump to Post

How would I test if it IS empty.
Because I am currently using: if (fileName == "")

And I am using this for over 6 if statments.

Didn't you familiar with logical not operator?

if (!*fileName)
// or
if (fileName[0] == '\0')
// or …
Jump to Post

All 6 Replies

Use memset...

memset(fileName, 0, MAX_PATH);

Rewrites the elements to 0.

Keep it simple:

fileName[0] = '\0';
// or even
*fileName = 0;
// to test:
if (*fileName) { // non-empty

;)

How would I test if it IS empty.
Because I am currently using: if (fileName == "")

And I am using this for over 6 if statments.

How would I test if it IS empty.
Because I am currently using: if (fileName == "")

And I am using this for over 6 if statments.

Didn't you familiar with logical not operator?

if (!*fileName)
// or
if (fileName[0] == '\0')
// or
if (strlen(fileName) == 0)

By the way, fileName == "" is equal to false in all cases. Why? Think about pointers comparison ;)

I know how to use logical operators, I just wasn't sure if !* was even valid.

the strlen() aproach seems best to me ArkM, I will try it.

Sorry for the DP, but I just want to let you know, ArkM, that that worked perfectly. Thanks to you to Yiucia, I used your memset approach. Rep for you all. Problem solved.

And just so you know, I am a decent programmer, don't be fooled by this simple question

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.