Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1049525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:37:48+00:00 2026-05-16T16:37:48+00:00

New to StackOverflow and new to C. I’m trying to take a struct as

  • 0

New to StackOverflow and new to C. I’m trying to take a struct as a parameter in a function ‘add_fields’, which adds the first two int fields ‘a’ and ‘b’ and puts the result in int field ‘c’. Not getting anything from the compiler, so obviously I’m doing something wrong. I just don’t know what. Any help would be appreciated.

    #include <stdio.h>

    struct add{
    int a;
    int b;
    int c;
    }

    void add_fields(struct add *d){
    d->c = a + b;

    }

    main(){

    struct add data;
    data.a = 1;
    data.b = 2;
    data.c = 0;
    add_fields(data);
    printf("%d + %d = %d", data.a, data.b, data.c);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:37:49+00:00Added an answer on May 16, 2026 at 4:37 pm

    You’re very close, but variables a and b don’t exist in that context, rather you need to access the fields via the pointer to struct d:

    d->c = d->a + d->b;
    

    Second, you need to pass a reference to the struct (since add_fields expects a pointer) when you call add_fields:

    add_fields(&data);
    

    The & indicates that you’re going to pass the address of the variable data rather than the value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After seeing the cool new reputation tab on the stackoverflow user page, I was
This question comes from my experience with the following question: https://stackoverflow.com/questions/492748/new-responses-icon-on-so-crashes-ie7-closed In that question,
New to both Ruby and Rails but I'm book educated by now (which apparently
New to Linux programming in general. I am trying to communicate with a kernel
I'm playing a little bit with the new StackOverflow API . Unfortunately, my JSON
I'm new to StackOverflow, so please let me know if there is a better
Hellow stackoverflow people, I am pretty new to Cocoa. I have XCode 3.1 Situation:
How can show closeable in the top of the page like stackoverflow new answers.
Im quite new to stackoverflow so I dont know if this question has been
I am new to stackoverflow i have doubt regarding calling jsp from javascript file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.