Taddis
Taddis

Reputation: 156

Accessing an array across files in C

I'm trying to access an array across files, like this;

int option[NUMBER_OF_OPTIONS];
...
addition(&option[0], num1, num2); 
...
printf("%d", option[0]);

thats the first(main) file

and the second is like this;

void addition(int * option, unsigned number1, unsigned number2)
{
int total = number1 + number2;
...
*option ++;
}

Something like that. Dont worry about the addition method.

The problem is that the printf method allways prints 0, as if *option ++; is never executed/read.

How do i fix this?

By the way, I get a warning in the "*option++;" file saying: warning: value computed is not used.

How do I solve this problem?

Thank you!

Upvotes: 0

Views: 146

Answers (3)

Nikos C.
Nikos C.

Reputation: 51840

This:

*option++;

doesn't do what you think it does. What it actually means is:

*(option++);

which first applies the increment operator to the option pointer and dereferences it afterwards. The effect is:

option++;
*option; // This is a statement with no effect, hence the warning.

You need this instead:

(*option)++;

Upvotes: 5

* binds looser than ++, so *option ++ = *(option++); to modify the values in array, you need to write (*option)++; that is, the suffix increment has higher precedence than the dereference operator

Upvotes: 1

nouney
nouney

Reputation: 4411

++ has a higher priority than *. So *option ++; is the same as *(option ++);, which do nothing (that's why you get the warning).

Try this:

(*option) ++;

Upvotes: 2

Related Questions