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

&つけが必要なvariableの正体

&付きvariableの正体
In the previous section, we explained how to display the address of a variable.
At that time, the variable names were displayed with an & preceding them.
この書き方は、第6章でscanf functionを使うHourにも使いました。

Essentially, variables with the & aren't just used when displaying addresses.
It's a writing style used in various places.

The nature of this & variable is very simple.
Actually, & is the operator that retrieves the address of a variable.
It's a symbol used to perform some kind of calculation, just like + or ×.

You can get the address of any variable using the & operator.
In other words, the & operator allows you to find the memory address of a variable.
Everything is pass-by-value.
In the preceding section, we explained that the & operator can be used to obtain the address of a variable.
But, what is the purpose of knowing the address of a variable?
Actually, this is related to the workings of functions in C.

第12章では、自作functionとvariableのScopeについてDescriptionしました。
Here, we explained how you can pass numbers to a function using a mechanism called arguments.
However, only numbers can be passed as an argument.
When a variable is specified when calling a function,
The value stored in the variable is copied to the actual arguments of the called function.

That means all the data passed to the function as arguments must be numeric.
This way of passing numbers as arguments is sometimes called pass by value.

キーワード
【Pass by value】

How to pass information to a function as a simple number.


The key point here is that even when you specify a variable as an argument, what's being passed is the underlying numerical value.
In a function that receives a number, it performs various calculations based on that number and returns the result.

"It usually works fine, but you'll run into trouble if you want to modify the contents of a variable directly."
Because the function receives only a copy of the variable's value.
No matter how much you modify that copy, it will have no effect on the calling variable.

"Therefore, we use the & operator to obtain the address and pass the numerical value of that address."
Then we would know the address of the caller's variable, which is the number in memory.
If we overwrite the memory location for that number, we can overwrite the caller's variable.
That's why you need to know the address of a variable.

Pass by reference
Pass by valueに対して、Pass by referenceという機能が存在する言語もあります。
This feature automatically handles the process of passing addresses.
C languageでは、Pass by valueしか出来ないのですが、
Passing an address is sometimes conventionally referred to as pass-by-reference.

scanf functionで&をつける理由
I think you should now understand why the & is used with the scanf function, given what we've discussed.
The scanf function is a function that takes input from the keyboard and stores it in variables.

However, as explained in the preceding paragraph, C only supports pass-by-value.
"That means you can only pass copies of values stored in variables to a function."
"This would prevent the variable from storing new values."

In that case, as explained in the preceding section, you can pass the variable's address as a number.
From the function's perspective, it can store data at that address.
Here's why you need to use & with variables in the scanf function.

However, what's questionable is that the & symbol wasn't placed before the array name when inputting the string.
This, as explained in the previous section, means that the array name represents the address of the first element of the array.
Therefore, the address-of operator (&) is unnecessary when passing an array name, as the array name itself indicates the array's location.

Essentially, you only need to specify the address of the array, even if it's not using the array name itself.
For example, you can enter data without using the array name.

Source code
#include <stdio.h>

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

The results of this program's execution are as follows:

Execution results
MARIO キーボードから入力した文字列
MARIO

This program only specifies the address of the 0th element instead of the array name.
No problem, they mean exactly the same thing.
Furthermore, you can even allow input from the middle of the array.

Source code
#include <stdio.h>

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

The results of this program's execution are as follows:

Execution results
QUEST キーボードから入力した文字列
DRAGONQUEST

In this program, we are assigning the string "DRAGON" to the array during the initialization phase.
In other words, characters have already been assigned to the array elements from index 0 to 5.
"Therefore, if you specify the address of the sixth element to the scanf function,"
The string is concatenated with the string starting from the 6th element.

"So, when you're only passing the array name, it certainly looks like you're not adding an &."
What's actually happening is precisely the same as specifying variables with an ampersand.


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