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"
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;
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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').
If you understand what you're doing, you're not learning anything.
itsme86 : you should take a looong vacation and give us a chance to reply to some threads
My Tutorials :
- Bad programming practices in : C
- C\C++ Tips
(constrcutive criticism is very welcome)
- Brain Cell
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
Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.
I beg to differ: http://jargon.watson-net.com/jargon.asp?w=automagicallyOriginally Posted by Stack Overflow
If you understand what you're doing, you're not learning anything.
Once again, enjoy shows how little he knows...
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
But at least they used code tags! Oh, wait...
Quzah.
Hope is the first step on the road to disappointment.
/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.Originally Posted by enjoy
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Use code tags plz, when using code.
-------
I see now, thank you very much Salem :)Originally Posted by Salem
Last edited by Kleid-0; 01-07-2005 at 06:55 PM.
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.Originally Posted by Kleid-0
Quzah.
Hope is the first step on the road to disappointment.
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 helpfulOriginally Posted by itsme86Code:char *set = "some string" set = "now i'm changed"
Warning: Opinions subject to change without notice
The C Library Reference Guide
Understand the fundamentals
Then have some more fun