-1

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()?

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328

2 Answers2

4

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.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

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

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62