I don't understand how this works:
Does the c compiler magically convert that into:Code:char *set = "abcdefghijklmnopqrstuvwxyz";
??Code:char *set = malloc(28 * sizeof(*set));
strcpy(set, "abcdefghijklmnopqrstuvwxyz"
Printable View
I don't understand how this works:
Does the c compiler magically convert that into:Code:char *set = "abcdefghijklmnopqrstuvwxyz";
??Code:char *set = malloc(28 * sizeof(*set));
strcpy(set, "abcdefghijklmnopqrstuvwxyz"
No.
It's more like
The "characters" are used to fill in an array of chars, and then the assignment makes a pointer to the first element of that array.Code:const char anonymous_name_generated_by_compiler[] = "abcdefghijklmnopqrstuvwxyz";
char *set = anonymous_name_generated_by_compiler;
Is almost the same thing (except you don't have to create a named location for the string)Code:const char temp[] = "abcdefghijklmnopqrstuvwxyz";
char *set = temp;
The main difference is that with malloc()'d memory, you can modify the string later on at some point. But the memory that is pointed to using char *set = "some string" can't be modified later on (i.e. you couldn't do set[0] = 'r').
itsme86 : you should take a looong vacation and give us a chance to reply to some threads :p
posted by kleid-0
Code:Does the c compiler magically convert that into:
Code:
char *set = malloc(28 * sizeof(*set));
strcpy(set, "abcdefghijklmnopqrstuvwxyz");
Code:
yes !!!
Um, compilers don't use magic. His first code is a string literal. His second is using a block of memory allocated to it.
The first code does not magically act like the second. The behavior's are different, and respond differently.
Want to make sure? Use the function free() on your second code after you malloc() it. Did it crash? Probably not. Try it on your first code. Did it crash? Yes. Because a string literal isn't your memory, so you can't modify it nor free it.
Big difference.
- Stack Overflow
I beg to differ: http://jargon.watson-net.com/jargon.asp?w=automagicallyQuote:
Originally Posted by Stack Overflow
;)
Once again, enjoy shows how little he knows...
But at least they used code tags! Oh, wait...
Quzah.
/b] sorry [/b]
Code:but i'm a student not a professonal programmer ... !
I know Quzah's humor is very subtle, but YOU REALLY DON"T NEED TO PUT CODE TAGS AROUND YOUR REPLIES.Quote:
Originally Posted by enjoy
Use code tags plz, when using code.
-------
I see now, thank you very much Salem :)Quote:
Originally Posted by Salem
We care. The creator of the forum cares. Everyone who has to read unindented piles of steaming (I am sillyI am sillyI am sillyI am silly) care. Use them or ... well find out fast that no one will help you ever again.Quote:
Originally Posted by Kleid-0
Quzah.
Ok I understand, I edited my last post. I need to start being more positive here.
You should point out (no pun intended) that char *set is immutable but it can still point to another area in memory. Not a big thing but helpfulQuote:
Originally Posted by itsme86
Code:char *set = "some string"
set = "now i'm changed"