MMGames Introduction to C C Language Development Environment C language now Useful Apps Contact Us
MMGames

Automatic version identification

SpineViewer

It's easy to tell by looking at it.

Response Time Checker

I can leave my computer on and do it.

Mouse cleaning time

I can leave my computer on and do it.

Mouse cleaning time

C language learned by suffering
C language learned by suffering

Variable to store address

The word pointer
In the previous section, we explained that passing the address of a variable to a function allows you to change the contents of the variable.
It requires a variable that can store the value of the address.
Without it, the address value cannot be passed as an argument.

In the public's mind, a variable that stores the value of an address is called a pointer.
This is really not a very accurate term.
This is because pointer is a generic term for three functions that handle addresses.
The term "pointer" is a generic term and is divided into exactly three types.

The first is the pointer type.
It is a type similar to the int and double types that have been mentioned.
However, pointer types have slightly different characteristics from those types.

The second is the pointer value.
This is a number, in essence an address, that can be handled by the pointer type.
Just as there is a distinction between numbers, such as integers and real numbers, there is a distinction between pointer values.

The third is a pointer variable.
This is a variable that is declared with a pointer type and can store pointer values.
It is basically the same thing as a variable of type int or a variable of type double.

In the next section, we will explain the differences between these three, so please refer to the
Please read carefully so as not to get confused.
pointer type
The pointer type refers to the type of variable that stores the address.
Pointer types have different aspects from normal types.
That is, pointer types are derived types created from other types.

This may sound like a confusing explanation, but in reality, it is not that difficult.
For example, in the case of an int type, it is an independent type. It has nothing to do with any other type.
In contrast, a pointer type is created by merging another type with a pointer type.

When an int type and a pointer type are combined, a type called pointer to int is formed.
In the case of a double type, a pointer to a double is created.
You can also merge a pointer type to int with another pointer type to obtain
It is even possible to create multiple pointer types, a pointer type of pointer to int.

In short, a pointer type cannot exist in this world without merging with other types.
It is, so to speak, a parasitic type.

By the way, a pointer type is, in essence, a type of variable that stores an address.
An address is, after all, only an integer value, as I had you display at the beginning of this chapter.
Why should a type that stores that integer value merge with another type?

There is a clear reason for this. The reason is to retrieve a number stored at a given address.
For example, int, double, and char types all have different sizes.
In the case of an int type, the standard compiler uses a size of 4 bytes, so
This means that we are using the equivalent of four lockers of memory, which exist in the hundreds of millions of units.
To retrieve a value of type int, these four must be retrieved together.

Depending on the type, even the binary numbers stored in memory can read differently, so the
So unless we know what the original type was, we cannot retrieve the stored number.

In other words, the pointer type must know what type of variable address it was.
So, if you make it as a pointer type in the form of merged with other variables in advance, you can use
The number stored in a variable of that pointer type is immediately recognizable as being of the merged type.

void pointer
In fact, there is a pointer type that exists by itself, the void type.
This type can store the address of any variable, but
Since the type of the original variable is not known, the value cannot be retrieved.

pointer value
A pointer value is a number that a variable of type pointer can store, but
This is, in essence, the value of the address of the variable.

In many compilers, a pointer value is simply an unsigned integer value.
The reason for this will be readily apparent if you remember how computer memory works.
Computer memory is a collection of billions of rockers.
The address is a number assigned to that locker.

However, one question remains here.
If the address is just an integer value, then storing it in an int type should be sufficient.
Why go to the trouble of treating it as a new number, such as a pointer value?

In fact, one could say that storing the data in an int type is a good idea.
On a 32-bit compiler, both the address and the int type will be 32 bits, so
There is nothing wrong with storing the data in an unsigned int type.
On the contrary, in language B, the ancestor of C, the
The mechanism was to store addresses in a type that corresponded to the int type in C.

However, there is a reason why it is treated as a number, a pointer value, in C language.
First, the meaning of a pointer value is obviously different from that of an ordinary integer value.

Normal integer values are numbers that are used to perform calculations, etc., in a program, while
This is because pointer values are not numbers used in calculations.
In other words, they are both simply integer values, but their purposes are completely different.

Because of this, there is no advantage in dealing with both in a variable of type int.
Rather, it becomes difficult to tell whether the number stored in the variable is an integer or a pointer value.
If this is the case, it would be much more convenient to treat the two as two separate numbers.
pointer variable
A pointer variable is an actual variable declared with a pointer type.
This variable can be freely assigned the address of a variable of its original type.
In addition, it can read and rewrite memory at the address it stores.

This shows that the properties of the variable are quite different from those of the previous variables.
In fact, pointer variables have features not found in variables up to now.

Other variables, whatever value they record, were intended to be used for some kind of calculation.
Even the character type char is really a character number, a variable used to calculate which character it represents.

With pointer variables, however, the stored address value is never used for calculation.
The role of a pointer variable is to calculate the value of memory at the address number it points to.
The purpose of a pointer variable is not to use the pointer variable itself in a calculation, but to calculate the variable it points to.

In other words, they usually behave as pointer variables, but
When a calculation of the variable pointed to is needed, it is necessary to transform into an ordinary variable.

This transformation function is the most important feature of pointer variables.
Pointer variables have two modes: pointer variable mode and normal variable mode.
They can switch between each as needed.

In pointer variable mode, there is not much functionality.
Specifically, there is only assignment to addresses and addition and subtraction.
This is because all that is needed in pointer variable mode is address storage.
As long as the address is memorized, there is no need to do anything else.

When switched to normal variable mode, its properties are exactly the same as those of a normal variable.
Thanks to this, calculations can be performed using a variety of operators just as with normal variables.
Naturally, the memory used at that time will be the address stored in pointer variable mode.

Up to now, variables could just be used as regular variables.
With pointer variables, you can switch between the two modes appropriately and
Remember that you must use them differently.


About this Site

The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or more complete than any book on the market.

Part 0: Program Overview
  1. What is the program?
Chapter 2: How to write a program
  1. Writing Rules
  2. Writing conventions
  3. Exercise 2
Chapter 3: Display on Screen
  1. String display
  2. newline character
  3. Exercise 3
Chapter 4: Numeric Display and Calculation
  1. Numeric Display
  2. Basic Calculations
  3. Type of value
  4. Exercise 4
Chapter 5: Numerical Memory and Calculation
  1. Memorize values
  2. Variable Type
  3. Type conversion
  4. Numeric justification
  5. Exercise 5
Chapter 6: Input from the keyboard
  1. Functions for input
  2. Fear of Input
  3. Exercise 6
Chapter 9: Repetition with a fixed number of times
  1. Sentences that repeat themselves
  2. Loop Operation Mechanism
  3. Exercise 9
Chapter 10: Unknown number of repetitions
  1. Loop of unknown frequency
  2. input check
  3. Exercise 10
Chapter 13: Handling Multiple Variables at Once
  1. Multiple variables are handled together.
  2. How to use arrays
  3. Exercise 13
Chapter 19: Dynamic Arrays
  1. Create arrays at will
  2. Exercise 19
Chapter 20: Multiple Source Files
  1. Minimal division
  2. The Stone of Partition
  3. Exercise 20

Comment
COMMENT

Open the 💬 comment submission box