Nick Mokas
Nick Mokas

Reputation: 1

Passing pointer from main to another function that passes the same pointer to another

My question is something simple. No structs, no objects. Nothing. I want to pass a variable from main to void func1 and func1 pass it to void func2 and save the changes.

Here is some coding, I would like to be correct.

int main (int argc, char *argv[]) {
int a = 2, b = 3, av, sum;

sumandav(a, b, &sum, &av);

printf("Average: %d\nSum: %d\n", av, sum);
return(0);

}

void sumandav(int a, int b, int *sum, int *av) {
*sum = a + b;
average(&sum, &av);
}

void average(int *sum, int *av) {
*av = (*sum)/2;
}

Thanks for your time.

Upvotes: 0

Views: 70

Answers (1)

smichak
smichak

Reputation: 4958

Replace the call to average with the following:

average(sum, av);

You're passing the pointers themselves, not pointers to the pointers.

Upvotes: 3

Related Questions