[SOLVED] Accessing variable value without declaring it in function prototype

Issue

In the function read, I need to access the values of integer a and integer b from the main function without declaring them in the prototype of the function read, using pointers.
Pointer x should point to integer a, and pointer y should point to integer b.

#include <stdio.h>
void read(int zzz[], int n) {
    int *arr = zzz, *x=a,*y=b;
}
int main() {
    int a, b;
    scanf("%d", &a);
    scanf("%d", &b);
    return 0;
}

How this could be implemented?

Solution

There are two ways that the read function can read the values or addresses of a and b:

  • Pass them as parameters
  • Make a and b global

So if you don’t want to make them parameters, you need to move them outside of the main function and before the read function.

Also, read is the name of a system function, so you should name it something else so you don’t conflict with it.

Answered By – dbush

Answer Checked By – David Marino (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *