2

I came across this code bellow today and I am not totally sure what it will do:

pLogFileCriteria->taskToLogFor[0][0] = *"*";

It's setting the value to a pointer to a local string that's on the stack? So when it exits this function, does that memory still exist? Seems fish to me.

0

2 Answers 2

4

The type of *"*" is const char. Your code snippet is assigning a char value to the location computed by the left-hand side.

This would do the same thing:

pLogFileCriteria->taskToLogFor[0][0] = '*';
Sign up to request clarification or add additional context in comments.

2 Comments

You are probably correct. It just seems like the people who wrote this code wanted to make it as complicated as possible.
@TyrelVanNiekerk If that's what they were going for, they didn't try very hard. It could be far more complicated.
1

String literals have static duration. They exist for the life of the program, thus obtaining a pointer to it's first character will remain valid after the function. But note the indirect operator. It will cause the pointer to the first character to be dereferenced, yielding that character.

1 Comment

So it's really just setting that character to '*' as james said in his comment.

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.