C language pointer, address, assignment three meanings

Source: Internet
Author: User

Let's start with a point of view. Let's see, right?

By: At the CSDN Forum, a friend of the altar mentioned this question:

====================================
Look at the code first:
#include <stdio.h>
void Main ()
{
int*p=10;
printf ("%d", p);
}

See what's wrong with the above code? I believe that the concept of the pointer, you know, int*p actually divided to see is (int*) p, he is actually a pointer, then int*p=10; equivalent to int*p;p=10; As you all know, the pointer is the address, the preceding statement means that the value of the constant 10 is assigned to the pointer p, by definition , this is not legal, because constants cannot be assigned directly to pointers, such as int a=10;int*p=&a;, which is legal. But I compiled it through the VC6.0 compiler, the file name is AL.C (note, non-CPP suffix), the result compiler no error, the output is 10.

====================================

It's really worrying to see this friend's post.

First, you say, "By definition, this is not legal, because constants cannot be directly assigned to pointers."

Where does this "definition" come from?

I said slowly ... Later, you will know that this "definition" of yours is meaningless.

First, what is a pointer.

You say, "Everyone knows, the pointer is the address." This statement is wrong (and harmful). Using this idea to understand pointers means that you have not yet experienced many of the real things about pointers.

The idea of "address" is to allow those entities (such as processors) that need to make data access to one or some of the storage units in memory to be able to find these storage units and introduce them.

Obviously, in these subjects, the location (i.e. address) of those storage units is also the data. The latter data, then, is stored and read and written in memory. (from the context of C programming, this latter type of data is a symbol of a pointer variable or a pointer constant.) )

The introduction of the concept of "pointer", compared with "address", is more complex, or, the former uses and meaning more diversity:

(1) The value of a pointer variable or a pointer constant can often be obtained by an address character (&) acting on the symbol of a variable or constant.

If you want to say "pointer value, cannot take constant address", then you are wrong again. You can make the pointer take to the address of the constant as follows:

int const a=12345;
int const *pa=&a;

Or
int const a=12345;
int const *PA;
pa=&a;

From this perspective, the purpose and meaning of pointers is to obtain the location of the data that the variable or constant symbol in the program actually corresponds to the memory.

So, for the same pointer volume, you can take the programmer's will, at any time, to get any existing symbols corresponding to the location of the data, as its value-but here is a very non-negligible condition, the following will say.

Address, there is no such meaning and purpose. The data corresponding to a symbol, in the memory position, in the original sign is declared, the appointment by the old boss to assign. This allocation process, which is transparent to programmers, is a significant distinction between high-level and low-grade languages. But because the "pointer" mechanism exists in C, it makes it "less advanced"-because programmers can use pointers to spy on how old bosses and their memory lovers are dating.

(2) The value of the pointer amount, in addition to the method mentioned in the above point (1), can also be obtained by the way in point (1), and then the amount of the minus minus, that is, the so-called "pointer operation" to be given. Let the function return a position value in a "useful" memory, usually in this category.

(3) In the example in point (1), we have seen that when declaring a pointer volume symbol, you must give a data type at the same time. This data type must be exactly the same as the data type that the pointer will point to when it is declared.

If the difference is a little bit, but still in the understanding of the old boss, then, the old boss will scold a sentence (spit a warning), and then he is not satisfied with the hands of the hand, for the amount of pointers to the right side of the thing, do some forced conversion.

But if the difference is more outrageous, the old boss simply went on strike.

In the example of this altar friend:

int*p=10;

Is the former "a little bit worse" situation. At this time, although the old boss did not strike (no compile error), but you do not know, he has some resentment in the secretly, for you to do something. If you turn on the warning option, you can hear his scolding. (Do not think that "compiled through, the program is written 100% to meet the standard"! )

If you put the above code, rewrite it like this:

int *p= (int *) 10;

Then, ensure that the old boss will not scold you, more will not strike.

What does it mean by writing like this? The original notation is to assign an integer of 10 to the pointer amount p. Let's just say that this 10 is an integer constant. In fact, no matter what the 10 is the amount of what type, as long as the front of the top one "(int *)", it will be cast by the old boss to: the symbol p at the declaration of the type set at the time (that is, pointer to the integer type variable).

Why do I say, "No matter what the 10 is, what type?"

You see:

int *p= (int *) ' a ';

This is the case, the old boss will not curse or strike.

Original acknowledgement: http://www.douban.com/group/topic/30597081/

C language pointer, address, assignment three meanings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.