B
Broeisi
Hello,
I wrote the tiny progam below just to understand arrays and strings
better.
I have 2 questions about arrays and strings in C.
1. Why is it that when you want to assign a string to an character array
that you must use the strcpy() function?
Why doesn't a assignment like gender = "male" not work for a character
array but for a pointer it works fine?
2. I declared a character array for 10 characters including the NULL
character, but as you see below I put more characters and it worked
fine. Can somebody explain me that please....
Thanks a lot in advance....
#include <stdio.h>
#include <string.h>
int main(void)
{
char *name;
char gender[10];
name = "broeisi";
strcpy(gender,"superb male");
printf("%s is a %s.\n",name, gender);
return 0;
}
This is the output of the program
broeisi@kitana Misc $ gcc arraystring.c -o arraystring
broeisi@kitana Misc $ ./arraystring
broeisi is a superb male.
I use gentoo linux with gcc version 3.4.4
I wrote the tiny progam below just to understand arrays and strings
better.
I have 2 questions about arrays and strings in C.
1. Why is it that when you want to assign a string to an character array
that you must use the strcpy() function?
Why doesn't a assignment like gender = "male" not work for a character
array but for a pointer it works fine?
2. I declared a character array for 10 characters including the NULL
character, but as you see below I put more characters and it worked
fine. Can somebody explain me that please....
Thanks a lot in advance....
#include <stdio.h>
#include <string.h>
int main(void)
{
char *name;
char gender[10];
name = "broeisi";
strcpy(gender,"superb male");
printf("%s is a %s.\n",name, gender);
return 0;
}
This is the output of the program
broeisi@kitana Misc $ gcc arraystring.c -o arraystring
broeisi@kitana Misc $ ./arraystring
broeisi is a superb male.
I use gentoo linux with gcc version 3.4.4