Jono
Jono

Reputation: 223

C Passing a pointer

static void increment(long long *n){
  (*n)++;
}

static void mult2(long long *n){
  (*n) = (*n)*2;
}

struct counter{
  long long counter;
};

struct counter* cp = malloc(sizeof(struct counter));
cp[0].counter = 5;
increment(cp);

printf("Expecting a 6 : %lld.\n", cp[0].counter);

Hi, This is a part of my code where I actually want to increment or x2 a counter, but I kept getting error on the argument and argument type.

Upvotes: 1

Views: 85

Answers (2)

Aryabhatta
Aryabhatta

Reputation:

struct count and long long are different types.

Try

increment(&(cp->counter));

Usage of cp[0].counter = 5 instead of cp->counter = 5 is quite bizzare, IMO.

Upvotes: 3

EboMike
EboMike

Reputation: 77762

You're passing a struct counter * into a function that expects a long long *. That won't work. You probably meant increment(&(cp[0].counter)).

Upvotes: 2

Related Questions