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

The identity of variables that need to be &trained

Identity of &ed variables
In the previous section, we explained how to display the address of a variable.
In this case, the variable name was displayed with & in front of it.
This writing style was also used when using the scanf function in Chapter 6.

In other words, a variable with & is not only used to display an address.
It is a writing style used in a variety of places.

The identity of this variable with & is very simple.
Actually, & is an operator that finds the address of a variable.
Like + and ×, it is a symbol for performing some kind of calculation.

For any variable, the & operator can be used to find the address of the variable.
In other words, the & operator lets you know the number of the variable in memory.
Everything is value-passing
In the previous section, we explained that the & operator can be used to know the address of a variable.
But what on earth do we need to know the address of a variable for?
Actually, this has to do with the C function mechanism.

In Chapter 12, we discussed homebrew functions and variable scopes.
Here we explained that a number can be passed to a function through a mechanism called arguments.
However, it is only numbers that can be passed with this argument.
If you specify a variable when you call a function, you can use the
The value stored in the variable is copied to the actual argument of the called function.

This means that all data passed to the function in arguments are numeric.
This method of passing numerical values as arguments is sometimes called value passing.

Keywords.
[Passing of value]

A method of passing information to a function as a simple number.


It is important to note that even if a variable is specified as a real argument, it is the numeric value of its contents that is passed.
When a function receives a numerical value, it performs various calculations based on it and returns the result as a return value.

Normally this is not a problem, but if you want to directly change the contents of a variable, you are in trouble.
Because what is passed to the function is merely a copy of the contents of the variable, the
No matter how much you change that copy, it has no effect on the calling variable.

So, we use the & operator to find the address and pass the numeric value of that address.
That way, the function knows the address of the caller's variable, i.e., its number in memory, and it can use the
If you rewrite the memory of that number, you can rewrite the caller's variable.
In other words, this is why we need to know the address of a variable.

pass by reference (e.g. to a friend)
In contrast to pass-by-value, some languages have a function called pass-by-reference.
This is a function that automatically handles the process of passing an address.
In C, only value passing is possible.
Passing an address is sometimes conventionally referred to as passing by reference.

Reason for & in scanf function
Given what I explained in the previous section, you can see why the scanf function adds &.
The scanf function takes input from the keyboard and stores it in a variable.

However, as explained in the previous section, the C language allows only value passing.
In other words, only a copy of the value stored in the variable can be passed to the function.
This makes it impossible to store new values in variables.

In this case, you can pass the address of the variable as a number, as explained in the previous section.
function can store data at that address.
This is the reason for the & on variables in the scanf function.

What is questionable, however, is that when entering a string, you did not prefix the array name with &.
This is because, as explained in the previous section, the array name represents the address of the first element in the array.
Therefore, the & is unnecessary because the location of the array can be determined by passing the array name.

In other words, in a sense, it is sufficient to specify the address of the array even if it is not the array name.
For example, the following will allow input without an array name.

source code
 #include <stdio.h>

int main(void)
{
    char str[256];
    scanf("%s", &str[0]); /* address of element 0 */
    printf("%s\n", str);
    return 0;
}

The result of executing this program will be as follows

Execution Result
MARIO String entered from keyboard
MARIO

This program simply specifies the address of element number 0 instead of the array name.
Both mean exactly the same thing, so there is no problem.
Moreover, you can even have the input come from the middle of the array by doing the following

Source code
 #include <stdio.h>

int main(void)
{
    char str[256] = "DRAGON";
    scanf("%s", &str[6]); /* address of element 6 */
    printf("%s\n", str);
    return 0;
}

The result of executing this program will be as follows

Execution Result
QUEST String entered from keyboard
DRAGONQUEST

In this program, the DRAGON string is assigned to the array in the initialization phase.
In other words, characters are already assigned to array numbers 0 through 5.
So, if you enter the scanf function with the address of the sixth element
Since the string is input from element 6, it is combined with the string from the beginning.

In other words, if you're only passing the array name, you're certainly not putting an & on the appearance of the array.
What you are actually doing is exactly the same as specifying a variable with &.


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 better 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