ある種の機能を持っているときに確認したいのですが
int subtract(int a, int b)
{
return a-b;
}
ポインタではなくsubtract(3,2)を呼び出すときに、値を渡します。
ありがとう、
ある種の機能を持っているときに確認したいのですが
int subtract(int a, int b)
{
return a-b;
}
ポインタではなくsubtract(3,2)を呼び出すときに、値を渡します。
ありがとう、
はい、そうです
int a
、整数を値で関数に渡すことを意味しますint* a
、関数への整数へのポインタを渡すことを意味します。だからこれのために
int subtract(int a, int b)
{
// even if I change a or b in here - the caller will never know about it....
return a-b;
}
あなたはこのように呼びます:
int result = substract(2, 1); // note passing values
ポインタ用
int subtract(int *a, int *b)
{
// if I change the contents of where a or b point the - the caller will know about it....
// if I say *a = 99; then x becomes 99 in the caller (*a means the contents of what 'a' points to)
return *a - *b;
}
あなたはこのように呼びます:
int x = 2;
int y = 1;
int result = substract(&x, &y); // '&x means the address of x' or 'a pointer to x'
はい、Cは常に関数パラメーターを値で渡します。ポインターを渡すには、ポインターのタイプを識別する星(アスタリスク)を指定する必要があります。
ポインタの場合でも、 Cは常に値関数パラメータを渡すことに注意してください。その場合、ポインタのアドレスが実際にコピーされます。
はい、値を渡しています。ポインターは、タイプ名の後、変数名の前にアスタリスクで示されます。