MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

Variable to store an address

The word "pointer"
In the previous section, we explained that passing the address of a variable to a function allows you to modify the contents of that variable.
It will require a variable that can store the value of the address.
Without it, I won't be able to receive the address value passed as an argument.

In the wider world, variables that store the address of a value are called pointers.
This isn't really an accurate way to describe it.
Because a pointer is a general term for a function that handles addresses.
The term "pointer" is a general designation and actually encompasses three distinct types.

The first is a pointer type.
It's a type similar to `int` and `double` that we've seen before.
However, pointer types have slightly different characteristics.

Secondly, it's the pointer value.
This is a number that can be treated as a pointer type, essentially an address.
Just as there's a distinction between integers and real numbers, there's a distinction between pointer values as well.

The third is a pointer variable.
This is a variable that can store a pointer value, declared as a pointer type.
It's essentially the same as dealing with int or double variables.

Moving forward, I will explain these three differences.
Please read carefully to avoid getting confused.
Pointer type
A pointer type refers to a variable type that stores an address.
Pointer types have different aspects compared to regular types.
It's the fact that pointer types are derived types created from other types.

It may sound complicated, but it's not really that difficult.
For example, in the case of an int type, it is an independent type.It has no relation to any other type.
In contrast, pointer types are created by combining other types with the pointer type.

Combining an integer type and a pointer type results in a pointer type to an integer.
"For a double type, a pointer to double is created."
Furthermore, combine an additional pointer type with a pointer type to an int.
You can even create multiple levels of pointer types, such as a pointer to a pointer to an int.

Essentially, pointer types cannot exist in this world unless they are combined with other types.
It's, in a way, a parasitic type.

By the way, a pointer type is essentially a variable type that stores addresses.
Addresses, in the end, are just integer values, as shown at the beginning of this chapter.
Why does the data type that stores that integer value need to be combined with other types?

There's a clear reason for that.To retrieve the numerical value stored at the specified address.
For example, int, double, and char all have different sizes.
"In the case of integers, they typically have a size of 4 bytes with standard compilers."
"It will use the space of four lockers worth of memory, amounting to billions."
To extract the integer value, you need to retrieve these four together.

Depending on the type, the way binary data stored in memory is interpreted can even vary.
"You can't retrieve the stored values unless you know what the original format was."

"Essentially, a pointer type needs to know the address of what type of variable it is pointing to."
Therefore, if you create it as a pointer type in advance, combining it with other variables.
It's immediately clear that the value stored in that pointer variable is of a combined type.

void pointer
実際には、単独で存在するPointer typeとしてvoid型があります。
この型は、どんなvariableのaddressでも記憶できますが、
元々のvariableの型がわからないため、値を取り出すことができません。

Pointer value
The value of a pointer is the numerical value that a pointer variable can store.
This is, essentially, the value of the variable's address.

In many compilers, a pointer value is simply an unsigned integer value.
The reason will become clear if you recall how computer memory works.
Computer memory is a collection of billions of lockers.
Because the address is the number assigned to that locker.

However, one question remains.
If the address were just an integer value, it should be sufficient to store it in an int type, shouldn't it?
Why do we need to treat pointer values as a new numerical value?

It's true, in a sense, that it could just be stored as an integer.
In a 32-bit compiler, both addresses and integers are 32 bits.
There's no problem storing it as an unsigned integer.
Moreover, in B, the predecessor of C,
"It was a mechanism for storing addresses in a type equivalent to the int type in C."

However, there's a reason why pointer values are treated as numerical values in C.
Firstly, a pointer value and a regular integer value have clearly distinct meanings.

"Integer values are numbers used in programs for calculations and other operations,"
Because pointer values are not numbers used in calculations.
In other words, while both of these are simply integer values, their purposes are entirely different.

"This means there's no benefit to handling both with int variables."
Instead, it becomes unclear whether the number stored in the variable is an integer value or a pointer value.
If that's the case, it would be more convenient to treat them as separate values.
Pointer variables
A pointer variable is a variable declared with a pointer type.
You can freely assign the address of a variable of the original type to this variable.
Furthermore, you can read and write to the memory at the address stored in B.

This reveals that it possesses properties quite different from previous variables.
In fact, pointer variables have capabilities that other variables do not.

Regardless of what values they recorded, the other variables were intended to be used in some calculation.
Even a character type like `char` is essentially a numeric code, a variable used to calculate which character it represents.

However, you never use the address value stored in a pointer variable in a calculation.
The purpose of a pointer variable is to calculate the value in memory at the address it points to.
The purpose of a pointer variable is to operate on the variable it points to, rather than the pointer variable itself.

In other words, it typically behaves as a pointer variable.
When a variable being pointed to requires calculation, it needs to be transformed into a regular variable.

This transformation feature is the greatest characteristic of pointer variables.
Pointer variables have two modes: pointer variable mode and regular variable mode.
You can switch between them as needed.

Pointer variable mode doesn't offer much functionality.”
Specifically, it involves only address assignment and addition/subtraction.
Because all that's needed in pointer variable mode is to store the address.
If you can just remember the address, there's nothing else you need to do.

When it switches to normal variable mode, its nature becomes exactly the same as a normal variable.
Thanks to this, you can perform calculations using various operators just like with regular variables.
Naturally, the memory used at that time will be the address stored in pointer variable mode.

So far, these variables could simply be used as regular variables.
With pointer variables, it's important to properly switch between two modes.
Please remember to use them appropriately.


About This Site

Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. newline character
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19