C string operations
-
if I have this code : char * s =(char*)malloc(10); and then I write 's="test"' then s will have a different address right ? why is this happening ?
-
if I have this code : char * s =(char*)malloc(10); and then I write 's="test"' then s will have a different address right ? why is this happening ?
s is a pointer, when you assign a value to s it should be an address not a literal. You allocate 10 bytes of memory and s holds the address of that memory. To assign a literal to the memory pointed to by s use *s = "test";
-
if I have this code : char * s =(char*)malloc(10); and then I write 's="test"' then s will have a different address right ? why is this happening ?
You are allocating assigning the address of the string literal "test" to the pointer
s
thus overwriting the address returned bymalloc
. You should be usingstrcpy
to copy the four characters into the allocated buffer. [edit] better terminology as pointed out by member below. [/edit] -
You are allocating assigning the address of the string literal "test" to the pointer
s
thus overwriting the address returned bymalloc
. You should be usingstrcpy
to copy the four characters into the allocated buffer. [edit] better terminology as pointed out by member below. [/edit]or, even better,
strncpy
. -
or, even better,
strncpy
. -
if I have this code : char * s =(char*)malloc(10); and then I write 's="test"' then s will have a different address right ? why is this happening ?
Member 10964099 wrote:
why is this happening ?
It append because it designed this way. That is how pointers work. I recommend to read THE book "the C Language" from Kernigan & Ritchie. The C Programming Language - Wikipedia, the free encyclopedia[^]
Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein
-
if I have this code : char * s =(char*)malloc(10); and then I write 's="test"' then s will have a different address right ? why is this happening ?
-
if I have this code : char * s =(char*)malloc(10); and then I write 's="test"' then s will have a different address right ? why is this happening ?
What all the others said and more generally rarely if ever do use an equal sign on a string in C at any other time than creation. There are functions required to add, copy, compare, split and search strings and you can't just use mathematical symbols like "=" and "+" to do things with strings like you can in some higher languages. Strings are not mathematical types and you can't use mathematical functions to do things with them. Those coming from a coding background in Java or Basic tend to do what you did and you have to unlearn it when using C because "string1" + "string2" will not work either, you will need to use strcat functions. String compares are likewise a function and you can't use mathematical equality signs to compare two strings. Learning the string functions, it is a requirement of learning the C language.
In vino veritas
-
You are allocating assigning the address of the string literal "test" to the pointer
s
thus overwriting the address returned bymalloc
. You should be usingstrcpy
to copy the four characters into the allocated buffer. [edit] better terminology as pointed out by member below. [/edit]Shouldn't that be: "you are assigning the address" instead of allocating?
-
Shouldn't that be: "you are assigning the address" instead of allocating?