简体   繁体   中英

C - changing values of a structure passed by reference to a function

The code below returns an "error: request for member 'height' in something not a structure or union". I would like to see bob 's height change from 4 to 5 after doStuff is called. Can anyone tell me why this doesn't work? Thanks!

#include <stdio.h>
#include <stdlib.h>

struct person{
    int height;
    int weight;
};

void doStuff(struct person *chris);

int main(){

    struct person bob = {4,4};
    doStuff(&bob);
    printf("%d", bob.height);

    return 0;
}

void doStuff(struct person *steve){

    steve.height = 5;
}

steve is a pointer to the structure, so instead of

steve.height = 5;

try

steve->height = 5;

or a bit more more cumbersome

(*steve).height = 5;

There is a syntax error in structure pointer usage

You have to use like this inside doStuff . Because steve is pointer variable here.

 steve->height = 5;

-> is Structure dereference operator

. is Structure reference operator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM