Pointers

Discussion in 'Programmer's Corner' started by Dritech, Feb 28, 2015.

  1. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    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.
     
  2. GopherT

    AAC Fanatic!

    Nov 23, 2012
    6,016
    3,789
    Yes, pointers to the variable's address by two methods. Read this...
    http://www.cplusplus.com/doc/tutorial/pointers/
     
  3. MrChips

    Moderator

    Oct 2, 2009
    12,439
    3,360
    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 (Text):
    1.  
    2. float CalcADD( float a, float b)
    3. {
    4.   return (a + b);
    5. }
    6.  
    7. float CalcSUB( float a, float b)
    8. {
    9.   return (a - b);
    10. }
    11.  
    12.  
     
  4. darrough

    Member

    Jan 18, 2015
    86
    19
    "*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.
     
  5. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    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);
     
  6. GopherT

    AAC Fanatic!

    Nov 23, 2012
    6,016
    3,789
    Read the link in post #2
     
  7. MrChips

    Moderator

    Oct 2, 2009
    12,439
    3,360
    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: Mar 1, 2015
  8. MrChips

    Moderator

    Oct 2, 2009
    12,439
    3,360
    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
     
    Dritech and killivolt like this.
  9. WBahn

    Moderator

    Mar 31, 2012
    17,737
    4,789
    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"
     
Loading...