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

Try using pointer variables

Declaration of pointer variables
In the previous section, we have discussed the three types of pointers, so the
Here, I would like to actually declare pointer variables to get a feel for them.

So, I'd like to quickly show an example of declaring a pointer variable.
In fact, this is another tricky thing.
For now, here are two examples of declaring a variable of type pointer to int.

source code
 int *p;
int* p;

This is how pointer variable declarations are written, as presented in many introductory texts.
These two are ways of writing to declare a variable of type pointer to an int named p.

The first one seems to be named *p, where * is a symbol meaning pointer type and
We are actually declaring a variable named p that stores the address of an int type variable.

It would seem, then, that the second declaration with the * in the type name would be easier to read.
If you declare more than one variable, the second and subsequent ones will differ from the apparent type name.
In the following example, the second p2 would be an ordinary int variable.

source code
 int *p1, p2;

It's a very troubling problem that is very confusing in both ways of writing.
For now, let's stick with the first way of writing here.
In other words, you can declare a pointer variable by prefixing the variable name with *.
At this time, the variable name is only p, even if it has a *.
Assign an address
As explained in the previous section, a pointer variable is a variable to which an address is assigned.
Next to declaring a pointer variable, we would like to assign an address as soon as possible.
By the way, assigning an address is fine, but what about the address to be assigned?

In theory, any number within the memory range of the computer's on-board
For example, a computer with 4 GB of memory can accept any number in the range of 0 to 4.2 billion.
Extremely simple computers like calculators or older video game consoles like the NES can be used that way.

However, what you are probably using to learn C is a modern personal computer.
Personal computers come with operating systems (OS) such as Windows, macOS, Linux, and
This is managed by a mechanism called virtual memory, which prevents memory from being used at will.

Keywords.
Virtual memory

A mechanism whereby the OS manages memory and allocates memory appropriately to a large number of applications.
In an environment where many apps are running simultaneously, if individual apps use memory at their own whim
The memory used by the different apps will overlap, and the apps will not be able to work properly.
The OS manages the memory used by individual applications so that they do not overlap.


Therefore, a textual address number will not be the address number managed by the OS.
If a pointer variable to which a textual address number is assigned is used, it will be judged by the OS to be an abnormal operation and will result in a forced termination.
Pointer variables must be assigned address numbers managed by the OS.

In fact, there is an easy and reliable way to assign a normally managed address number.
The method is simple: declare another variable and assign its address.
Since the declared variables are created in memory areas managed by the OS, they can be used without problems.
The following program is an example of assigning the address of a variable to the pointer variable p.

source code
 int main(void)
{
    int *p;
    int i;
    p = &i;
    return 0;
}

First, you can declare a variable as a pointer variable by prefixing it with *.
Understand that an i without a * in front of the variable name is an ordinary variable.

In this example, the & operator is used to find the address of variable i and assign it to the pointer variable p.
In other words, at this stage, the pointer variable p contains the address of i.
This means that the address of i and the contents of the pointer variable p should naturally be the same.
The following program is an example of using the printf function to print the address to verify.

source code
 #include <stdio.h>

int main(void)
{
    int *p;
    int i;
    p = &i;
    printf("p = %p\n", p);
    printf("&i = %p\n", &i);
    return 0;
}

The results of running this program might be as follows

Execution Result
p = 0012FF80
&i = 0012FF80

The values are spectacularly the same.
This is in a sense natural, given that pointer variables are also variables.
Because we are assigning &i to p and displaying its value immediately afterwards.

However, note the type here.
The type of the pointer variable p is of the type pointer to int.
The type of variable i is int, but the address obtained using the & operator is a pointer type.
Thus, &i can be assigned to p, and both can be displayed with the %p specifier.

null pointer
A pointer variable is also assigned a bullshit value immediately after it is declared.
Since we have no idea whether the value is a usable address or not
Using that address by mistake will surely result in a bug.

To prevent this, it is necessary to distinguish whether an address is assigned or not.
Therefore, the C language provides a null pointer.
By assigning the symbol NULL to the pointer variable
You can indicate that the address has not been assigned, i.e., it is not yet ready for use.
int *p = NULL;
In this way, an if statement comparing whether p == NULL will show that
It is possible to distinguish whether an address is assigned to p or not.


Is the null pointer 0?
int *p = 0;
a null pointer is also assigned.
This is a fixed part of the C syntax.
It does not mean that NULL is 0. A NULL is still a NULL.
NULL is an identifying value to indicate that a correct address has not been assigned.
It is to be clearly distinguished from 0, which is a number to be used in calculations.
However, most compilers will set NULL to 0...

Mode Switching
As explained in the previous section, pointer variables have two modes.
There are two modes: normal variable mode and pointer variable mode.
If you are using a pointer variable without specifying anything in particular, you are in pointer variable mode.

To switch to normal variable mode, place a * sign in front of the variable.
A pointer variable marked with a * symbol has exactly the same function as a normal variable.
The following program is an example of using a pointer variable by switching it to normal variable mode.

source code
 #include <stdio.h>

int main(void)
{
    int *p;
    int i;
    p = &i;
    *p = 10; /* assign to pointer variable switched to normal variable mode */
    printf("*p = %d\n", *p);
    printf("i = %d\n", i);
    return 0;
}

The result of executing this program will be as follows

Execution Result
*p = 10
i = 10

This program switches to normal variable mode by appending * to the pointer variable p.
*p is the pointer variable p that has switched to normal variable mode.
As long as it is *p, it can be treated exactly like a normal variable.

A pointer variable that has switched to normal variable mode functions just like a normal variable, but
The memory used at that time is the address assigned while in pointer variable mode. In other words, the memory used by

The most basic use of pointer variables
Assign the address of the memory you want to read or write to while in pointer variable mode.
Then switch to normal variable mode to manipulate that memory.

is the most basic use of a pointer variable.

Instead of directly specifying what number of memory should be rewritten
Assign the address of the memory to be rewritten, switch the mode, and rewrite.
This may be difficult to understand intuitively because of the two-stage structure, so to speak.

In the previous program, the address of variable i is assigned to the pointer variable p in line 5, and
In line 6, p is switched to normal variable mode and 10 is assigned to the address stored by p.
At this time, the address stored by p is, in other words, the address of variable i, so
As a result, the value of variable i is rewritten to 10.

To be a little more specific, at this point, variable i and *p in normal variable mode
It means that they are using exactly the same memory area.
Instead of saying that assigning 10 to *p automatically switches i to 10 as well
The two represent the same memory location to begin with.


Bizarre symbols*.
The * symbol has really three different meanings, which can cause confusion.
Let us clarify the distinction between the three.

The first is the multiplication operator. This is the so-called multiplication.
It is the symbol used in an expression, as in kai = 5 * 8.

The second is the indirect reference operator. It puts the pointer variable in normal variable mode.
It is a symbol used in expressions, as in *p.
Since multiplication is not possible with pointer variables when in pointer variable mode, the
It is distinguishable even though it uses the same symbol as the multiplication operator.

The third symbol is used when declaring a pointer variable.
It is used only at the time of declaration, as in int *p.
This is where things get complicated, as there is an indirect reference operator * that switches to normal variable mode, and the
The * symbol used in the declaration is a completely different symbol that has nothing to do with anything.
They just happen to use the same letter.

I think it should be easier to understand if we use different letters for all three.
Assigning the same characters is one of the flaws of the C language.
But there is no way to fix it now.
You all should be well aware that these three are symbols with different meanings.

shortcut
Up to the previous section, all functions of pointer variables have been explained.
In fact, pointers have only the functions described in the previous section.
When in pointer variable mode, you can assign a memory address to the
Switching to normal variable mode and then manipulating that memory, this is the full function of the pointer.

Having understood all this, there is a question that naturally arises.
After all, what useful function is a pointer?
As in the previous section, you can assign the address of a variable in pointer variable mode and then use the
What is the point of such a tedious process of switching to normal variable mode and manipulating them?

This is already as you have wondered, and there is no use for such usage.
It is much easier and less error prone to manipulate variables normally.

The real use of pointers is to use them as shortcuts.
It is the same as those shortcuts that line the Windows desktop.

A shortcut is a file that points to a file somewhere else.
Opening the shortcut opens the file it points to.
Nevertheless, since the shortcut is not the file itself that it points to
Shortcuts can be created anywhere and
Creating or deleting multiple files has no effect on the file pointed to.

This is precisely the role of the pointer itself.
If you store in a pointer variable the address of a variable that actually exists, then you can use
Wherever that pointer variable can be used, even where the original variable cannot be used.
If the pointer variable is switched to normal variable mode, it can be used just like the original variable.
This is exactly how shortcuts work.

Pointers in other languages
It is generally believed that pointers are a C and C++-only feature.
Certainly, this is true in the sense that they manipulate a specified memory address.

But the real use of pointers is to use them as shortcuts, and
From that perspective, **most practical languages have pointers**.
Java's references are just such a feature, and they are used frequently, and
The same can be said for VisualBasic's SET statement, for example.

Without pointers, there would be no way to create linked lists, tree structures, and so on.
Complex data structures cannot be realized, and object orientation is also difficult.
In this sense, it is better to use pointers in Java or VisualBasic, where the mechanism is unclear, than in
C pointers are easier to understand because they have a clear structure.

The biggest difference between references in other languages and pointers in C is whether they are automatic or manual.
While references in other languages are almost automatic and act as shortcuts
C pointers are completely manual and must be fully understood and used by the programmer.

Instead, when an advanced programmer becomes proficient with C pointers, he or she can use pointers alone to
It is too powerful a feature to be able to realize almost any control structure, any data structure, and so on.
In fact, most of the functionality of the C language is based on pointers.



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.