zZShort_CircuitZz
zZShort_CircuitZz

Reputation: 185

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;
}

Upvotes: 1

Views: 2879

Answers (2)

AlexD
AlexD

Reputation: 32576

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;

Upvotes: 2

mahendiran.b
mahendiran.b

Reputation: 1323

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

Upvotes: 2

Related Questions