0

I'm trying to print the outcome of the function below

void add_votes(struct votes* v1, struct votes* v2) {
    v1->men += v2->men;
    v1->women += v2->women;
}

With this set of code

add_votes(all, michigan);
    printf("all->men: %d\n", &(all->men);

Here are all needed variables and structure

struct votes {
    int men;
    int women;
};
struct votes *michigan = {200, 300};
struct votes *all = {0, 0};

I tried placing an & before the cast in the printf but after a second printf it displays the address of the pointer and when I print it without a & it doesn't display nothing

If it’s supposed to be a pointer how should I do it And if I remove the & from the print it says a series of number I guess the address but I need the value It says something like 1303720 when I need it to be 200 or something like that

1
  • Should just be printf("all->men: %d\n", all->men); You don't care about the address at all. Note, though, that you are not correctly initializing your pointer variables; they have to point to something. Commented Nov 7, 2020 at 19:08

1 Answer 1

4

Your variable declarations are wrong. They should be structures, not pointers.

struct votes michigan = {200, 300};
struct votes all = {0, 0};

Then you pass the addresses of the structures to the function:

add_votes(&all, &michigan);
printf("all.men: %d\n", all.men);
Sign up to request clarification or add additional context in comments.

2 Comments

Ok but how is the function add_votes going to look if I make it all normal structures it's not assigning values
add_votes doesn't change. It's receiving pointers to these structures, so it updates the contents.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.