Zombie Army Trilogy PC Patch – Full Version +53 DLC; . Sacred 3 Orcland Story Addon-RELOADED-PC RELOADED-VERSION ; .
Download Patch ; . Sacred 3 Orcland Story Addon-RELOADED. zip : Sacred 3 Orcland Story Addon-RELOADED..Q:
is there a way to add a char to a char array?
int main()
{
char a[5];
char c;
printf("enter a char:");
scanf(" %c", &c);
a[0] = 'a';
a[1] = 'b';
a[2] = 'c';
a[3] = 'd';
a[4] = 'e';
printf("%c", a[2]);
return 0;
}
When I try this code I get a segmentation fault. How can I edit a to store c?
I would also like to know why my original code works when I use int instead of char.
A:
With C, you can't modify a string. If you need to do such an operation, you need to do it in multiple steps:
char *p = "hello";
// The string 'hello'
p = "world";
// The string 'world' is now the new value of p
You can do the following:
#include
int main() {
char *a = "hello";
*a = 'w';
// a now contains "world"
return 0;
}
Or you can use memcpy() to copy the string:
#include
int main() {
char *a = "hello";
char b[] = "world";
memcpy(a, b, 6);
// a now contains "world"
return 0;
}
More information about strings can be found here.
The Weekly Standard reserves the right to use your email for internal use only. Occasionally,
we may send you special offers or communications from carefully selected advertisers we believe may be of benefit to ac619d1d87
Related links:
Comments