Frank Vilea
Frank Vilea

Reputation: 8477

C: Simple pointer question

I want to program an access function that returns username and password. Here's what I came up with:

#include <stdio.h>

char *
getMySQLUsername()
{
    return "myUsername";
}

char *
getMySQLPassword()
{
    return "myPassword";
}

int main()
{
    printf("%s\n", getMySQLPassword());
}

It seems to work, but is this code correct?

Upvotes: 0

Views: 160

Answers (1)

Sydius
Sydius

Reputation: 14257

You should return const char * because you cannot change a literal string. You're also not returning anything from main, which is only valid in C as of C99 and in C++.

Upvotes: 8

Related Questions