Initialization array of char in C

Hello,

I want to initialization an array of char in C and I don't want to use a pointer.
How can I fix it?

Thanks

Error in image:

https://i.ibb.co/85BH4tX/Annotation-2020-07-01-122908.jpg

1
2
3
4
5
6
7
8
9
struct  Employee 
{
	char name[100];
	char family[100];
	char userrname[] = {'a','d','m','i','n','\0'};
	char password[] = "1234";
	int numberOfAccount;
	int deposit;
};
outside of a struct, password is the best way IMHO.
username also works but its silly to do that to yourself.
you can also use a pointer:
char * foo = "foo";
(I know you said no pointer but c-strings don't require memory allocation/destruction via special syntax / parsing of the syntax). I do not like the * approach either, but without the memory management its unlike other pointer efforts.

I am not sure you can set the values in the type. And, you need to be careful; if you plan to change the username or password later, what if they put in a 10 character password? You only created space for 4 chars this way. I am out of practice at C but I think you need to give bigger [size]s for these and find another way to initialize them. Consider:
http://www.c4learn.com/c-programming/c-initializing-structure/
Last edited on
In C++, apparently you need to specify the size of the array explicitly when doing inline class initialization.
1
2
3
4
struct Employee {
	char userrname[6] = {'a','d','m','i','n','\0'};
	char password[5] = "1234";
};


I don't think you can do that in C at all, though. The next best thing would be to use a factory or an init function + strcpy or similar.
Last edited on
Topic archived. No new replies allowed.