cpointerspass-by-referencedereferenceaddressof

Why passing by reference works using only pointers declared in the argument list of this C program?


This is Program 3.7 from Robert Wood's "C Programming for Scientists and Engineers". In the code below, the pointers are not declared and initialized in the body of main(), but directly in the argument list of read_data() function.

Normally, when writing code, I first declare 3 pointers (int *int_ptr, float *float_ptr, char *char_ptr) and then assign the addresses of the a, b and c to them. But in the program below, "address of" operators are passed when calling read_data(&a, &b, c); through the argument list of void read_data(int *int_ptr, float *float_ptr, char *char_ptr){...}, where we have "contents of" operators, which seems inconsistent, but works. Also, fscanf(stdin,"%d %f %s", int_ptr, float_ptr, char_ptr); stores the data supplied by the user in the address of the pointers, whereas I think thought that they should be written in the contents of the variables a, b and c like that:

fscanf(stdin,"%d %f %s", int_*ptr, *float_ptr, *char_ptr);

But I am wrong.

Otherwise the program works, but if I had to write it this way, I will not be able to. So, I want you to help me understand and maybe provide alternative solution.

/* Program 3.7- Use of functions for input and output of data */
#include <stdio.h>

int main(void) {
  int a;
  float b;
  char c[11];

  /* function prototype */
  void read_data(int *, float *, char *);
  void write_data(int, float, char[]);

  /*call the function*/
  read_data(&a, &b, c);
  write_data(a, b, c);

  return (0);
}

/*Function: read_data - reads an int, float and char string*/
void read_data(int *int_ptr, float *float_ptr, char *char_ptr) {
  fprintf(stdout, " Supply an integer, a float and a string (max. 10 chars):");
  fscanf(stdin, "%d %f %s", int_ptr, float_ptr, char_ptr);
  return;
}

/* Function: write_data - displays an int, float and char string*/
void write_data(int i, float j, char k[]) {
  fprintf(stdout, " The supplied data is: %d %f %s\n", i, j, k);
  return;
}

Solution

  • It's not inconsistent, you are just inferring the wrong attributes to the function.

    Note the difference between:

    A function declaration:

    void read_data(int *int_ptr, float *float_ptr, char *char_ptr){...}
    

    And a function call

    read_data(int_ptr, float_ptr, char_ptr);
    

    But in the program below, "address of" operators are passed when calling read_data(&a, &b, c); through the argument list of void read_data(int *int_ptr, float *float_ptr, char *char_ptr){...}, where we have "contents of" operators, which seems inconsistent, but works

    Your inference on what this function's argument list represents is what is causing your confusion. The function does not take "contents of" pointers, but rather pointers themselves. And since the "address of" operator returns a pointer, it is perfectly fine to call the function using read_data(&a, &b, c)