Looking at this, I'm pretty sure that you trying to store "H"
(which is a constant in C) that is of type int into insert2. In other words, your trying to store a type int
(sizeof("H") == 2
on my compiler) into a char (sizeof(char) == 1
). From there, it is up to your compiler to allow this behavior or throw an error. Plus "Hello world"
is an immutable string that is located only in ROM, so you can't directly modified the string anyways.
To demonstrate, I ran:
printf("val:%d\n", "H");
printf("val:%d\n", "He");
printf("val:%d\n", "He");\\note 2nd instance the same
printf("val:%d\n", "Hel");
printf("val:%d\n", "Hell");
printf("val:%d\n", "Hello");
printf("val:%d\n", "Hello ");
printf("val:%d\n", "Hello W");
printf("val:%d\n", "Hello Wo");
printf("val:%d\n", "Hello Wor");
printf("val:%d\n", "Hello Worl");
printf("val:%d\n", "Hello World");
Yields:
val:4196404
val:4196406
val:4196406\\note 2nd instance the same
val:4196409
val:4196413
val:4196418
val:4196424
val:4196431
val:4196439
val:4196448
val:4196458
val:4196469
As @Rasmus was saying:
char * insert2 = (char *)(space+50);
strcpy(insert2 , "Hello world\0");// the '\0' if changing strings
Should resolve the issue.
BUT, I do not see much practical use for this, besides being vague/indecipherable, and not to mention wasteful of memory (unless you calculate it all out), so I recommend just using a struct
:
struct blockOfMem{
int num;
char* str1;
char* str2;
};
....
struct blockOfMem space;
space.num = 5;
strcpy(space.str1, "Hello world\0");// the '\0' if changing strings
strcpy(space.str2, "Goodbye friend\0");// the '\0' if changing strings
This makes you code much more readable and practical to use.