This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 8 years ago.
What's the effect of int a(); in C++?
Is it equivalent to int a or int a(0)?
And how about char c() and double d()?
What's the effect of int a(); in C++?
That declares a function, with no parameters, that returns an integer.
Is it equivalent to int a or int a(0)?
No. Each of these declares a variable of integer type; the second also initialises it with the value zero.
And how about char c() and double d()? Thanks.
These also declare functions, with different return types.
int a(); is a function declaration.
int a is declaring a to be of type int.
int a(0) is declaring a to be of type int and initialising it to 0.
char c() and double d() are function declarations also returning char and double respectively.
All the function declarations should be terminated by a ;.
Neither, it declares a function.
Related
This question already has answers here:
Array changed in a void function, is still changed outside! why? (scope)
(4 answers)
Passing an array as a parameter in C
(3 answers)
Passing Arrays to Function in C++
(5 answers)
Passing an array as an argument to a function in C
(11 answers)
Closed 11 months ago.
I don't get it why you can alter the values inside the array, without using a reference or a pointer (&, *), I'm a freshmen student, and I don't know the reason behind, I hope someone can provide a logical answer, please refer to the code below, Thank You in Advance.
#include <iostream>
using namespace std;
void a(int x[]){
for(int i = 0; i < 5; i++){
x[i] += 2;
}
}
int main(){
int x[5] = {1,2,3,4,5};
a(x);
for(auto b : x){
cout << b << " ";
}
return 0;
}
A function parameter is never an array in C++. When you declare a function parameter as an array, it is automatically adjusted to be a pointer to element of such array. These declarations are effectively identical:
void a(int x[]); // looks like an array of int of unknown bound
void a(int* x); // declaration above is adjusted to this
void a(int x[1234]); // the size is ignored completely
An array implicitly converts to a pointer to the first element of the array (such conversion is called decay). Hence, you can call the function that doesn't accept an array parameter by passing an array argument:
int* ptr1 = x; // implicit conversion
int* ptr2 = &x[0]; // same conversion explicitly
a(x); // same conversion happens here
These two rules (function parameter adjustment and array to pointer decay) make it so that what syntactically looks like passing arrays by value, is actually done using indirection. Within the function, you modify the elements by indirecting through the pointer parameter that points to the array that exists outside of the function.
Important note: The adjustment of array to pointer in function parameter does not apply in other contexts. Arrays and pointers are distinct types with different properties.
Another note: The adjustment does not apply to parts of compound types. For example, a function parameter of type "pointer to array" will not be adjusted to be "pointer to pointer" and "reference to array" will not be adjusted to be "reference to pointer".
The parameter having the array type in this function declaration
void a(int x[]){
is adjusted by the compiler to pointer type to array elements type. That is the above declaration is equivalent to
void a(int *x){
In this call of the function
a(x);
the array designator is implicitly converted to pointer to its first element. That is the call is equivalent to
a( &x[0]);
So within the function you have a pointer to the first element of the array declared in main.
Using the pointer arithmetic you can access elements of the array. That is the elements of the array are passed to the function by reference in the C meaning indirectly through a pointer to them.
Within the function the variable x has the type int *. And this expression statement
x[i] += 2;
is equivalent to
*( x + i ) += 2;
Beacuse
void a(int x[]){
is the same as
void a(int *x){
and so you are using a pointer
Why?
Because an array like
int x[10];
'decays' to a pointer when passed to a function (and in other places). This can be very confusing but at the same time is very flexible
It mens that I can have a function like strlen that can accpet a 'real' array, or a pointer. These 'strings'
char *s1 = malloc(10);
strcpy(s1, "hello");
char s2[] = "hello";
char *s3 = "hello";
store their data in different ways but all can be handled by
size_t strlen(const char *s);
note that this is exactly the same as
size_t strlen(const char s[]);
they are 2 different ways of writing the same thing. Personal preference is for the second type if its really is an 'array' vs a pointer to maybe an array.
One issue with this 'decay' is that inside strlen (or any pointer/array accepting function) it is impossible to 'know' the length just from the parameter. The compiler knows that the size of s2 is 6 but this information is not carried forward to the function.
Regularly SO sees this
void my_func(int *arr){
int len = sizeof(arr)/sizeof(arr[0]);
....
}
int a[10];
my_func(a);
This will give len = 1 or 2 (depending on 32 or 64 bit machine), never 10
The flexibility costs a litle power
This question already has answers here:
Placement of the asterisk in pointer declarations
(14 answers)
Closed 3 years ago.
Pointer variables are confusing to me. Consider the code below:
int main() {double* grade; double *grade;}
double* fn() {}
What is the difference between double* grade and double *grade?
What is the difference between double, double* in int main(), and double* of fn()?
These are the same:
double* grade;
double *grade;
Both define a variable of type double*. These are also same:
double*grade;
double * grade;
double * grade ;
double
*
grade
;
In many cases, white space is optional in grammar of C++. Note that your program is ill-formed, since there are two variables defined with the same name in the same scope.
double* fn() {}
This is definition of a function that returns double* and has empty argument list. This particular function has undefined behaviour because there is no return statement despite the function not returning void.
There is no difference between double* grade; and double *grade;
double* grade; defines a pointer to double.
double grade; defines a variable of type double.
double* fn(); declares a function that returns a pointer to double
This question already has answers here:
Is an array argument passed to a function not a constant pointer?
(6 answers)
Closed 4 years ago.
a and b are both array name. why is ++b is permitted while ++a is not.
int main(void)
{
int a[3][3] = { 0 };
foo(a);
return 0;
}
void foo(int b[][3])
{
++b;
}
When you declare an argument such as int b[][3], what the compiler really translates it as is int (*b)[3]. That is, b is a pointer and not an array.
Note that this translation only happens for function arguments, and only for the first "dimension".
This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 8 years ago.
I recently wrote below simple program but compiler shows warning.
#include <iostream>
int main()
{
int a();
std::cout<<a;
return 0;
}
[Warning] the address of 'int a()' will always evaluate as 'true' [-Waddress]
What is the meaning of the above warning? Why value of a is 1 not 0?
It might look like a definition of a as an int, but:
int a();
declares a function a taking no parameters and return int.
Use:
int a{};
instead.
std::cout<<a;
calls operator<<() with bool which is always nonzero, hence true.
int a(); declares a function, not a variable. If you want a to be a zero-initialised variable, then you'll need one of
int a{}; // C++11 or later
int a = int();
int a(0);
int a = 0;
<< doesn't have an overload that can directly take a function; so it looks for a suitable conversion sequence to a type that it is overloaded for, and finds:
int() -> int(*)() -> bool
that is, using the standard function-to-pointer and pointer-to-boolean conversions. The function pointer won't be null, since a declared function must exist and have an address; so the boolean value will be true.
This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 8 years ago.
What's the effect of int a(); in C++?
Is it equivalent to int a or int a(0)?
And how about char c() and double d()?
What's the effect of int a(); in C++?
That declares a function, with no parameters, that returns an integer.
Is it equivalent to int a or int a(0)?
No. Each of these declares a variable of integer type; the second also initialises it with the value zero.
And how about char c() and double d()? Thanks.
These also declare functions, with different return types.
int a(); is a function declaration.
int a is declaring a to be of type int.
int a(0) is declaring a to be of type int and initialising it to 0.
char c() and double d() are function declarations also returning char and double respectively.
All the function declarations should be terminated by a ;.
Neither, it declares a function.