Why is array not pointer?


	char a[2] = {'x', 0};
	char *p = a;

By saying p is a pointer, we mean the number saved in p’s address is an address, correct?
But the number saved in a’s address is not an address, at all. That’s my explanation why array is not pointer.

To dig deeper, consider this:
1. when you ask a’s rvalue, it returns it’s address, which is all the confusion comes from.
this is what happens when you do things like

		char *p = a;
		func(a); //func take char* as a parameter

2. when you ask a’s address, again, it returns it’s address. This is somewhat like a’s lvalue.
This why it’s so hard to clearly answer the question “does array have lvalue?”.
But still, my answer has to be that array doesn’t have lvalue.

Advertisements

What does extern REALLY mean?


While it’s often to see this:

extern int i;

the usage of ‘extern’ is not so obvious, and there is more.

Here is my question:
I have 1.cpp with this piece of code:

extern const int i;
int main()
{
	printf("%d", i);
}

How do you define i in 2.cpp so that the program links and runs? Answer is listed below.

This should be in 2.cpp

extern const int i = 2;

The key to understand it is that extern is for declaration, instead of definition.
extern declares the variable (and function) external linking.
However, it has a significant side effect,
which is taking away definition while leaving there only declaration. see this example:

extern int i; //here we are only taking advantage of extern's side effect.

The side effect works only if i is not initialized. Therefore

extern int i = 2; //this is definition. side effect does't work.

Now back to our original question. The code in 2.cpp is equivalent to this.

extern const int i; //declaration
const int i = 2; //definition

I hope now it’s all clear.

However, another question has been bothering me for long time. I need your help.
Is there a way to declare (without definition) a static variable?
So far my answer is no.

auto keyword has changed


In Visual Studio 2010 with default compiler options,
this is ok:

auto i = 0;

But this is error.

auto int i = 0;

The library builders want it more than application developers.
So many interesting features in C++11.

Why cannot .(dot) be overloaded but -> can?


Here is the fundamental reason:

struct CMyClass
{
	void f(){}
};
void test()
{
	CMyClass a;
	a.f();
}

For the variable a, you do ‘a.’ but you are not allowed to do ‘a->’.
In another word, ‘a->’ is available, and ‘a.’ is already occupied.
If c++ allowed dot to be overloaded, much confusion would occur, because it’s already occupied.
Here is one example of the confusions: http://www2.research.att.com/~bs/bs_faq2.html