开发者

C Passing a pointer

开发者 https://www.devze.com 2023-03-01 16:21 出处:网络
static void increment(long long *n){ (*n)++; } static void mult2(long long *n){ (*n) = (*n)*2; } struct counter{
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开发者_JS百科 argument type.


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.


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)).

0

精彩评论

暂无评论...
验证码 换一张
取 消