Woo! troll time. Let's go through these points one by one.
Craze Frog wrote:
And WHICH IDIOT made a SYSTEM programming language WITHOUT putting the BIT SIZES of the standard types into the standard? A valid 32-bit C compiler for x86 can, while following the standard, have an 8-bit int and a 64-bit long long (as long as a short and char is no more than 8 bits either). Only conventions keeps the programs running, not the standard.
How is a language supposed to be portable and stand the test of time if the size of it's standard types are explicitly set in stone? Do you think when C was written the average size of an integer was 32-bits? (answer: noooooo).
C's pointer "system" (where's the system?) is a total mess. The * belongs to the variable, not the type, so notptr will not become a pointer. However, even though the * belongs to the variable and not the type, it can also be specified when you specify only a type and no variable, like in a function prototype:
In one declaration the * belongs to the variable, in the next it belongs to the type. What were they thinking? (Were they even thinking?)
The '*' qualifier (it's a qualifier in this case and not an operator) states that the variable following it will be defined as a
pointer. That is:
ptr is a
pointer to a variable of type integer.. Notptr is a
variable of type integer. The '*' qualifier 'belongs' to the variable.
Now to your second example:
This is lax coding on your part, but allowed by the C99 standard. Inside the parameter list, you define one argument: "an integer". Again, the '*' operator kicks in afterwards: "the following argument will be a pointer to the type already specified". Then what? well, you just didn't name your argument. There is one there, but you just omitted to name it. This is allowed by the C99 standard, but it doesn't change the way in which the semantic rules of the language operate.
And if it's not enough that the *, which is the dereference operator, does not always perform a dereference, the address of operator is crazy as well. (Yes, I know it's not the dereference operator when used there, but there is absolutely no reason why it shouldn't be. Disallowing writing to address 0 is an arbitary restriction imposed by the operating system and C is supposed to be portable.) But back to the address of operator (or more the lack thereof): (Note, C++, but shouldn't matter) Code:
Code: Select all
void add_one (int &x) {
x += 1;
}
void (*inc)/(int &x) = add_one; /* a */
void (*inc)/(int &x) = &add_one; /* b */
Both a and b does the same thing. Let me ask: since when was the address of X the same as X?
Woo, Woo! Let the truth train continue!
First off I'm not sure what you were smoking when writing this example - where the hell did the forward slash '/' come from between "void (*inc)" and "(int &x)"? That's not valid and it doesn't compile. Anyways, ignoring that...
As stated, '&' as a
qualifier (note the use of that word again) denotes that "the variable following will be defined as a reference". A reference is essentially a pointer without the dereference syntax. It's a less powerful pointer, essentially.
'&' as an operator, is, as you said the 'address of' operator. Now, take a look at your code. Not having to take the address of a function to obtain a function pointer is a known issue and is deprecated. Some (C++) compilers will emit a warning, some an error (note C++ compilers are usually stricter than C. This holds for G++/GCC). The fact that you *can* do it doesn't mean you *should*. The
reason for it's behaviour is - what
should this code return?
Code: Select all
<don't worry about the type> x = add_one;
What is the value and type? there is none. You can't pass around functions as variables in C (without using function pointers), and there is no logical 'value' a function should take. So most compilers just default back to returning a function pointer, as if you'd used the '&' operator.
The fact that I can write "unsigned long letter;" in C doesn't have any advantages in programming.
Why the hell not?
Back to the +=. In C you don't use = for comparison, right? You just use it for assignment, right? Not really. In the following operators = is used for assignment: <blah blah long rant>
You are confusing yourself. '=' is the assignment operator.
All of the other two-character combinations you mentioned are operators in their own right, not variations on the assignment operator.. I hope that cleared it up
Disallowing writing to address 0 is an arbitary restriction imposed by the operating system and C is supposed to be portable.)
Dude, where are you getting your facts from?
Code: Select all
unsigned int *myptr = (unsigned int *)0x0;
*myptr = 66; // This will ususally cause a GPF and kill your process.
Where exactly can't you write to address zero? C++ compilers (as they're stricter) will usually stop you dereferencing a pointer which hasn't been initialised, as it contains junk and will make your program nondeterministic (which, unless you're writing a random number generator, is not what you want.)
Truth train pulling into the station - all change please!
JamesM