Namara

Code daily. Without assist.

2026-08-20

greeting.c

const char *greeting(void) {
    char msg[] = "hello";
    return msg;
}

This compiles but is broken. What's wrong, and how do you fix it with a minimal change?

Answer

msg is a local array; it's destroyed when greeting returns, so the returned pointer dangles — using it afterward is undefined behavior. Give it storage that outlives the call: static const char msg[] = "hello"; return msg;