I need to search through a piece of memory for a character string, but some of these strings separate each character, for example:
“I..am ...... ring“
with all. ”These are zero characters. My problem is because that it gets into memory. I tried several ways, for example:
char* str2;
str2 = (char*)malloc(sizeof(char)*40);
memcpy((void*)str2, "123\0567\09abc", 12);
It will put the following into memory: str2 points to: 123.7.9abc..
Something like
str2 = "123456789\0abcde\054321";
Will have str2 pointing to a block of memory that looks like 123456789.abcde,321where '.' is a null character, and ',' is the actual comma.
Thus, clearly inserting null characters in cstrings does not work as easily as I thought, for example, inserting a newline character. I ran into similar difficulties trying this with a string library as well. I could do individual tasks, for example:
char* str;
str = (char*)malloc(sizeof(char)*40);
strcpy(str, "123");
strcpy(str+4, "abc");
strcpy(str+8, "ABC");
But this, of course, is not preferable, and I believe that the problem lies in my understanding of how c-style strings are stored in memory. Obviously, "abc \ 0123" does not actually go into memory as 61 62 63 00 31 32 33(in hexadecimal format). How is it stored and how can I store what I need?
(I also apologize for not setting the code in blocks, this is my first question asking a question, and is somehow “four times” harder than I can apparently handle. Thanks, Lucian. Necessary .)