However, in order to understand the concept of pointers that we will be dealing with shortly,
You need to understand how variables are stored in memory.
As explained in the previous section, computer memory is like a giant, single-row locker.
And each of these lockers remembers a state of on or off.
We refer to a single locker as one bit, and a group of eight lockers as one byte.
Memory locations are numbered and distinguished on a byte-by-byte basis.
Variables declared in a program are also stored in this memory with assigned numbers.
"It's just that it would be difficult to distinguish them just by number, so we're giving them names."
When compiled into an executable, variable names are converted to numbers.
In short, all variables are created in memory.
First, please keep in mind that they are being distinguished by numbers.
In the preceding section, we explained that all variables are created in memory and distinguished by their assigned numbers.
Actually, the numbers assigned to variables can be checked within the program.
Finding the number is surprisingly easy; you just need to use the %p specifier with the printf function.
However, you must prepend an ampersand (&) before variable names.
The following program is an example that displays the number of the int variable i.
Source code
#include <stdio.h>
int main(void)
{
int i;
printf("%p\n", &i);
return 0;
}
The output of this program may be as follows.
Execution results
0012FF80
These are the results obtained in the author’s environment, although the numerical values may vary.
It depends on the computer and compiler being used.
Which memory address is assigned to a given variable.
which is automatically determined by the compiler (or, more accurately, the linker),
Furthermore, this value can also change due to the operating system's virtual memory functionality.
This number is in hexadecimal notation.It is 1245056 in decimal.
That's because it's displayed in a hard-to-understand format like hexadecimal.
As explained in the preceding paragraph, computer numbers are stored in binary.
In binary, the number of digits increases by powers of two (2, 4, 8, 16, and so on).
In base-10, the number of digits increases as you move along (e.g., 10, 100, 1000, and so on).
In hexadecimal, the number of digits increases with 16, 256, 4096, and so on.
As you may notice, decimal and binary don't mesh well.
Hexadecimal is convenient because it works well with binary.
However, here, hexadecimal representations aren't particularly important.
The key is that the assigned numbers for the variables are easily accessible.
The number assigned to this variable is called an address.
キーワード
【address】
The numerical address in memory assigned to a variable.
You can think of it as having the exact same meaning as a URL address.
The number that indicates the location of a variable is what we call an address.
Numbered variables
In the previous section, we only examined one variable, but this time we will examine the addresses of multiple variables.
Just to reiterate, what we're calling the locker number assigned to a variable, we're referring to as the address.
The following program is an example of displaying the addresses of three integer variables.
Source code
#include <stdio.h>
int main(void)
{
int i1, i2, i3;
printf("i1(%p)\n", &i1);
printf("i2(%p)\n", &i2);
printf("i3(%p)\n", &i3);
return 0;
}
This is an example run in the author's environment.The numbers vary depending on the environment.
It appears that the sequential numbers are assigned with an offset of 4 to int variables.
It's simply because the size of an int is 4 bytes.
"Since 4 bytes is 4x8=32, it's 32 bits, or 32 digits in binary."
"It can store numbers from 0 to 4294967295 (approximately 4.3 billion) when negative values are not included."
"In the case of 2 bytes (16 bits), the range is from 0 to 65535."
Variables of types other than int also have different sizes depending on the compiler.
However, a char data type is always exactly 1 byte.
"to store character codes within the range of 0 to 255,"
"That's because you need to make sure it's always up to 255, or you'll run into problems."
いにしえのパソコンの中にはcharが7ビットというものも存在したらしいですが・・・
This time they are consecutive, but the addresses of variables do not always have to be consecutive.
Because these three variables are used separately.
It doesn't matter if we're in different places.
Some compilers may produce reversed order.
Array indices
Like variable numbers, arrays can also display addresses.
Tedious as it may be, an address is the locker number assigned to a variable.
For arrays, you don't need to add & at the beginning.
"However, since individual array elements are treated like variables, it naturally requires the & operator."
The following program is an example that displays an array and the addresses of its elements.
Source code
#include <stdio.h>
int main(void)
{
int array[10];
printf("array___(%p)\n", array);
printf("array[0](%p)\n", &array[0]);
printf("array[1](%p)\n", &array[1]);
printf("array[2](%p)\n", &array[2]);
return 0;
}
It appears that arrays are also allocated in consecutive blocks of 4 bytes.
Furthermore, it seems that when an array name is specified, it resolves to the index of the array's first element.
In fact, all the tricks of the array are revealed here.
In fact, the array name represented the address of the array's first element.
"When referring to each element, I labeled them with numbers like [0], [1]. This means..."
This means to refer to the memory address of array name + element number.
Essentially, if you establish an initial address, you can increment it by adding a number to it.
It allows you to express a state where numerous variables are lined up.
However, what's a bit peculiar here is that the actual address increases by 4 when the element number is 1.
"This mystery should be solvable immediately if you remember the size of an int."
In a 32-bit compiler, the size of an int is 4 bytes.
In essence, the address of the next integer variable is the initial address plus 4.
Similarly, the address of the int variable two positions ahead is the initial address + 8.
We will explain the workings of this array in more detail later.
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.