Basic and Advance C Question:
Why ca not I do something like this?
Answer:
Why can't I do something like this?
extern char *getpass();
char str[10];
str = getpass("Enter password: ");
Arrays are ``second-class citizens'' in C; one upshot of this prejudice is that you cannot assign to them . When you need to copy the contents of one array to another, you must do so explicitly. In the case of char arrays, the strcpy routine is usually appropriate:
strcpy(str, getpass("Enter password: "));
(When you want to pass arrays around without copying them, you can use pointers and simple assignment.
extern char *getpass();
char str[10];
str = getpass("Enter password: ");
Arrays are ``second-class citizens'' in C; one upshot of this prejudice is that you cannot assign to them . When you need to copy the contents of one array to another, you must do so explicitly. In the case of char arrays, the strcpy routine is usually appropriate:
strcpy(str, getpass("Enter password: "));
(When you want to pass arrays around without copying them, you can use pointers and simple assignment.
Previous Question | Next Question |
Is a pointer a kind of array? | Is NULL valid for pointers to functions? |