1
int main(int argc, char **argv) {
  char username[256];
  username = ?;
}

thanks~

1
  • 4
    What if I pass 257 characters? Commented Jan 11, 2011 at 4:03

2 Answers 2

7

You probably want to make username be a pointer, then you can just assign it:

int
main(int argc, const char *const *argv)
{
    const char *username;
    if (argc >= 2)
        username = argv[1];
    else
    {
        fprintf(stderr, "usage: %s username ...\n", argv[0]);
        return 2;
    }
    /* ... */
}

But if you really want to copy the contents of the array, you are looking for strncpy.

Sign up to request clarification or add additional context in comments.

Comments

2

If you want to copy another string (like argv[1]), then you probably want to use strncpy ( http://strncpy.org/ ). If you want to input it from stdin, then http://en.wikipedia.org/wiki/Fgets is probably a better bet.

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.