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".
Related
This question already has answers here:
C++ returning an assignment?
(1 answer)
Using assignment as a condition expression?
(5 answers)
What does assignment operator means in return statements, like return t =...?
(5 answers)
Closed 1 year ago.
I am starter of C++. When I study the opensource, I see the function.
int foo(int a, int b){
return a=b;
}
But I only know the function return such as...
int foo(int a, int b){
return a;
}
what is the meaning of this "return a=b" statement?
This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 7 years ago.
Below is a C program:
#include<stdio.h>
void func(int a[])
{
printf("%d\n",sizeof(a));
}
int main()
{
int arr[10];
printf("%d\n",sizeof(arr));
func(arr);
return 0;
}
Now the problem is that the sizeof() operator returns 40 when I use sizeof(arr) but 8 when I use sizeof(a).
What is the reason for this behavior?
Simple, yet confusing: A function parameter such as int a[] or int a[42] gets adjusted to int* a. func(int a[]) is adjusted to
void func(int* a)
Here, a really is no more than a pointer. So sizeof(a) is sizeof(int*).
On top of that, arrays can decay to pointers in many situations, such as when you pass arr to func. That allows this to happen
func(arr);
even if func's parameter is a pointer.
You are comparing size of a "pointer" to the actual size of an array.
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.
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.