C++ -- return x,y; What is the point?
The comma operator is primarily used in for
statements like so:
for( int i=0, j=10; i<10; i++, j++ )
{
a[i] = b[j];
}
The first comma is not a comma operator, it's part of the declaration syntax. The second is a comma operator.
Why should you include more than one value in a return statement?
The statement return 2 * 3 + 1, 1 + 5;
returns the value 6.
This the trick of comma operator in C++. You can read more about it here:
https://en.cppreference.com/w/cpp/language/operator_other
A comma operator is basically a list of expressions separated by commas, they will be evaluated from left to right, and the result of the last item will be treated as the result of the whole comma operator.
Here is a simple example demonstrating how comma operator works.
int foo() {
int i = 1;
return i += 2, i++, i + 5; // This is a comma operator with three items
// i += 2 will be evaluated first, then i == 3
// i++ will be evaluated second, then i == 4
// i + 5 will be evaluate last, and the result is 9
// the result of the last item is returned by the return statement
}
int main() {
std::cout << foo();
return 0;
}
This code prints 9.
Getting the address of function return arguement in C
You are returning a temporary copy of a point and take his address is not a good idea.
Try this:
struct point* makepoint(int x, int y);
int main(int argc, const char *argv[]) {
int i;
int number1 = 5, number2 = 10;
struct point* points[MAXPOINTS];
for (i=0; i< MAXPOINTS; i++)
points[i] = makepoint(number1, number2);
for (i=0; i< MAXPOINTS; i++)
free(points[i]);
return 0;
}
struct point* makepoint(int x, int y) {
struct point* my_point = malloc(sizeof(struct point));
my_point->x = x;
my_point->y = y;
return my_point;
}
Anyway, in your code:
struct point *points[10];
for (i=0; i< MAXPOINTS; i++) {
points[i] = &(makepoint(number1, number2));
}
...you have an array of 10 pointers and you're trying to assign 1000 pointers (MAXPOINTS).
How do function pointers in C work?
Function pointers in C
Let's start with a basic function which we will be pointing to:
int addInt(int n, int m) {
return n+m;
}
First thing, let's define a pointer to a function which receives 2 int
s and returns an int
:
int (*functionPtr)(int,int);
Now we can safely point to our function:
functionPtr = &addInt;
Now that we have a pointer to the function, let's use it:
int sum = (*functionPtr)(2, 3); // sum == 5
Passing the pointer to another function is basically the same:
int add2to3(int (*functionPtr)(int, int)) {
return (*functionPtr)(2, 3);
}
We can use function pointers in return values as well (try to keep up, it gets messy):
// this is a function called functionFactory which receives parameter n
// and returns a pointer to another function which receives two ints
// and it returns another int
int (*functionFactory(int n))(int, int) {
printf("Got parameter %d", n);
int (*functionPtr)(int,int) = &addInt;
return functionPtr;
}
But it's much nicer to use a typedef
:
typedef int (*myFuncDef)(int, int);
// note that the typedef name is indeed myFuncDef
myFuncDef functionFactory(int n) {
printf("Got parameter %d", n);
myFuncDef functionPtr = &addInt;
return functionPtr;
}
What does the question mark character ('?') mean in C++?
This is commonly referred to as the conditional operator, and when used like this:
condition ? result_if_true : result_if_false
... if the condition
evaluates to true
, the expression evaluates to result_if_true
, otherwise it evaluates to result_if_false
.
It is syntactic sugar, and in this case, it can be replaced with
int qempty()
{
if(f == r)
{
return 1;
}
else
{
return 0;
}
}
Note: Some people refer to ?:
it as "the ternary operator", because it is the only ternary operator (i.e. operator that takes three arguments) in the language they are using.
Output of the following C program
This is a result of short-circuit evaluation.
The expression ++x
evaluates to 2
, and the compiler knows that 2 || anything
always evaluates to 1
("true") no matter what anything
is. Therefore it does not proceed to evaluate anything
and the values of y
and z
do not change.
If you try with
x=-1;
y=z=1;
You will see that y
and z
will be incremented, because the compiler has to evaluate the right hand side of the OR to determine the result of the expression.
Edit: asaerl answered your follow-up question in the comments first so I 'll just expand on his correct answer a little.
Operator precedence determines how the parts that make up an expression bind together. Because AND has higher precedence than OR, the compiler knows that you wrote
++x || (++y && ++z)
instead of
(++x || ++y) && ++z
This leaves it tasked to do an OR between ++x
and ++y && ++z
. At this point it would normally be free to select if it would "prefer" to evaluate one or the other expression first -- as per the standard -- and you would not normally be able to depend on the specific order. This order has nothing to do with operator precedence.
However, specifically for ||
and &&
the standard demands that evaluation will always proceed from left to right so that short-circuiting can work and developers can depend on the rhs expression not being evaluated if the result of evaluating the lhs tells.
Function return type conflict in C
The main problem here is that the div
function that you've declared conflicts with a function defined in a header (stdlib.h
). Renaming the function to ddiv
, for example, will fix that particular issue.
The second problem, as other commenters have noted, is that you have put forward declarations inside the body of main, so the function avg
cannot see the definition of ddiv
. Best practice here is to move all the forward declarations out, or just have the main
function declared after the definition of the other functions and omit the prototypes entirely.
The third problem, as a matter of correctness, is that your div
function is operating on integers, so will not give the correct answer for fractional amounts (so will return 50
instead of 50.5
for avg(50,51)
). Instead, you either need to have the div
function take floats, in which case the compiler will automatically do the right thing, or modify the function to compute (float)x/y
.
The last problem is that you are using %d
in the printf
to print out a floating point number. Most likely the compiler will warn about this, but if not, you will get strange behavior. Use %f
instead.
Related Topics
How to Efficiently Wait for Cts or Dsr of Rs232 in Linux
How to Get a List Video Capture Devices Names (Web Cameras) Using Qt (Crossplatform)? (C++)
Dealing with Library Dependencies on Linux
What Is The Correct Behavior of Pthread_Mutex_Destroy When Destroying a Locked Mutex
Instantiating Classes by Name with Factory Pattern
Capturing H264 Stream with Opencv
Cudamalloc of a Structure and an Element of Same Structure
How to Start Process on Linux Os in C, C++
Template Metaprogramming: (Trait For) Dissecting a Specified Template into Types T<T2,T3 N,T4, ...>
Cross-Platform C++: Use The Native String Encoding or Standardise Across Platforms
Sdl/C++ Opengl Program, How to Stop Sdl from Catching Sigint
Dynamically Loading Static Library
Interactive Console Programming for C/C++
C or C++ Bigint Library on Microsoft Windows