Historical background of pointer expression in C language

Asked 2 years ago, Updated 2 years ago, 38 views

Many people say that it is difficult to understand the pointer in C language.
I think one of the reasons for this is the pointer notation.

For example, the meaning of * is different when you declare a pointer and when you assign an address.

int*pointer; indicates that pointer is a pointer and
a=*pointer; indicates that * retrieves the value stored in the pointer address.

The question I have here is
The way int*pointer is expressed is why you didn't or couldn't use *.
For example, const or static, you might have written point before int.

Also
The pointer pointer points to the contents with *pointer and represents the address with pointer but
I think the other way around was better.
On the other hand, is there any harm?

I don't know enough about C language yet, so I think there are some things that I can't do.
I would appreciate it if you could keep company.

c

2022-09-30 19:45

2 Answers

The early C compiler may be a clue to the historical issues.Below is an introduction to this article.

Bookworm: The first C compiler written by Dennis Ritchie is available on GitHub

By this time, there was already a pointer.However, the pointer-like declaration int**ipp; is not possible, and the normal pointer declaration seems to have written intip[] at first (although the expression int*ip; was accepted).

Below is my opinion.

The variable C is always an object with some kind of storage (although the storage is not always in memory, and often optimization implies that the variable itself was not present), meaning that the entity that binds the variable is contained in the variable's storage space.There is no variable that has a different storage space between the entity and the variable itself, such as the Java or C# reference type.The same structure as a variable can be transformed into a simple data operation on memory, so the compiler can also be very simple to create.

To handle sequential data, such as strings, you need an array-like mechanism.It is possible to have an entire array as a entity, but if you try to show the same array to another variable, multiple variables cannot share the same entity in the first place, and copying the entire array is inefficient.Also, the fact that the life of the variable and the object always match makes it difficult to dynamically generate and destroy data with variables that are statically determined at compile time.That's why it's called a pointer.By allowing access to an array to be treated the same as that of a pointer (not to say that the array and the pointer are exactly the same, these two are strictly distinguished), array manipulation through the pointer has been enabled.In addition, dynamically reserved storage space such as malloc was accessed through a pointer to separate the life of the variable from the life of the object in the dynamically reserved storage space.

What's important is that the pointer is only one substance.Thus, without complicating its linguistic structure, C enabled powerful expressions using pointers.It was very important to say that the technology of the time was simple because the study of compilers and parsing was not as advanced as it is now.We didn't incorporate the complexity of having a separate Java or C# reference model (I don't know if there was any language like that at the time).

Now, how do you describe a pointer?As the early C compiler saw, * tried to represent the object to which the pointer indicated, but the declaration was in the form of intip[];.Since pointers and arrays can be treated in the same way, I think it was meaningful to write like an array declaration.Now, intip[];, which can also be called int(ip[]);.In other words, when ip is considered an array and the element is accessed, the type is int.From there, int(*ip); that is, int*ip; is considered a pointer, and the object type to which it points is int.This means that it has the same interpretation as * where it is not a declaration (interpretation that it is the entity to which the pointer points).By doing so, it is presumed that they may have tried to simplify the parsing.I don't know if it worked at that time.

If you look at the language after that, you can think that this method was a failure.The expression (int*) can be used as an argument for casts and sizeof, which also led to the misunderstanding that the declaration was seen as a lump in int*.The arrangement will be delayed, so it will be even more confusing.I think that's the reason why Java and others put type expressions like int[] in the preface (although Java also supports postscript writing because it's a C/C++ user).Recently, however, it has been considered that it is not appropriate to set the model beforehand, and I think that the number of languages (Go, Rust, etc.) afterwards is increasing.


2022-09-30 19:45

First of all, the following is my personal opinion, and I am sorry if there is a mistake.

Is this understanding correct?The pointer simply "points" the address to the end, and it is up to the programmer to decide how to handle the address information they know.

In other words, since only information that "information is stored from this place on the memory" is passed, it is left to the program side how many bytes of data are read from the value in the previous memory to be used as data.In other words, the specified a=*pointer is just handing over the address.The value is not passed.Does the value appear to be because, for example, the function handling this address or the built-in function implement the process of "cutting out information from the address indicated by the pointer (memory space) with a specific rule?"

With that understanding,

A * in
int* pointer; indicates that the pointer is a pointer.
* of a = *pointer; indicates that the pointer is a pointer indicating the "address" to be passed to pointer a.

I believe that is consistent.


2022-09-30 19:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.