Pointers

Thread Starter

Dritech

Joined Sep 21, 2011
901
Hi all,

Can someone please explain to be how the pointer in the program below actually operate?

Are *add and *sub referring to the memory location of variables add and sub? or are they pointers?
If they are pointers, why are then not declared? (i.e. float *add, *sub; )

Any help would be appreciate.
 

Attachments

GopherT

Joined Nov 23, 2012
8,009
Hi all,

Can someone please explain to be how the pointer in the program below actually operate?

Are *add and *sub referring to the memory location of variables add and sub? or are they pointers?
If they are pointers, why are then not declared? (i.e. float *add, *sub; )

Any help would be appreciate.
Yes, pointers to the variable's address by two methods. Read this...
http://www.cplusplus.com/doc/tutorial/pointers/
 

MrChips

Joined Oct 2, 2009
30,824
The function Calc( ) is attempting to modify the contents of two variables. They solve this by the use of pointers.

A simpler solution would have been to return the value of one math operator at a time.

Code:
float CalcADD( float a, float b)
{
  return (a + b);
}

float CalcSUB( float a, float b)
{
  return (a - b);
}
 

darrough

Joined Jan 18, 2015
86
"*add = a + b" reads "add a and b and store at the address passed through the parameter *add".

*add could have been named *x and it would work the same. If it begins with a * that means it is carrying an address and the function is to work with the variable at that address.
 

Thread Starter

Dritech

Joined Sep 21, 2011
901
Thanks a lot for the replies :)

Another quick question. Can someone explain the use of &item in the lines below?
These lines are used to read and write form different files.
fscanf indicate a read command and fprintf indicate a write command. "inp" and "outp" are the respective pointers, but what are ""%lf", &item" and ""%.2f\n", item" exactly doing please?

fscanf(inp, "%lf", &item);
fprintf(outp, "%.2f\n", item);
 

GopherT

Joined Nov 23, 2012
8,009
Thanks a lot for the replies :)

Another quick question. Can someone explain the use of &item in the lines below?
These lines are used to read and write form different files.
fscanf indicate a read command and fprintf indicate a write command. "inp" and "outp" are the respective pointers, but what are ""%lf", &item" and ""%.2f\n", item" exactly doing please?

fscanf(inp, "%lf", &item);
fprintf(outp, "%.2f\n", item);
Read the link in post #2
 

MrChips

Joined Oct 2, 2009
30,824
Part 1
passing parameters by value vs by reference

In the function call:

Calc ( a, b , &x, &y);

we see two ways of passing parameters, by values a and b
and by reference, &x and &y.

a and b are input values to the function. The values of a and b (copies and not the variables themselves) are placed in a container (on the stack or register) before calling the function. This is called passing by value.

The function is attempting to alter the contents of the variables x and y. In order to do that we need to know where the variables reside. Hence we pass the addresses of the variables x and y.

& is called the address operator.

Hence &x is the address of x.
&y is the address of y.

Thus, before the function is called, the addresses of x and y are placed in a container (stack or register) and then the function is called.

This is called passing by reference.

The fscanf( inp, "%lf", &item) statement is attempting to alter the value of a variable item.
Hence we have to pass the address of item.
 
Last edited:

MrChips

Joined Oct 2, 2009
30,824
Part 2
fscanf and fprinf formatting

These are text input and output functions. Hence we need to specify how the text is to be formatted.

fprintf(outp, "%.2f\n", item);

"%.2f\n" is the format specifier which says you want the floating point number to be displayed with 2 places after the decimal point.

\n requests that after the text is displayed, move to a new line.

See this:

http://www.tutorialspoint.com/c_standard_library/c_function_fprintf.htm
 

WBahn

Joined Mar 31, 2012
30,083
A simple way to keep these straight in your mind is to read them as:

&x => "the address of x" or "the address at which x is stored"
*x => "the value stored at address x"
 
Top