1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematic operations and structures in C

  1. Jan 26, 2008 #1
    Hello guys, I was wondering:

    say you have a structure:

    struct tag{

    int a
    char b
    int b
    int c

    how would you make int c equal to int a + int b for all variables of type struct tag? I have tried using pointers but it baffles me how you would make a global change in that all variable s of type struct tag would be affected, rather than just specific variables.

    thank you for your time.
  2. jcsd
  3. Jan 26, 2008 #2
    *variables rather than variable s
  4. Jan 26, 2008 #3


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    You'd have to perform the assignment t.c = t.a + t.b for every variable of type tag that you create. There is no such thing as a "global change."

    If you decide to use C++ features, you can turn this structure into a class, and then use a member function to calculate c, rather than storing it directly.

    - Warren
  5. Jan 26, 2008 #4
    If you want to do that it would look like:

    Code (Text):
    struct tag{

    int a;
    char b2;
    int b;
    int c() { return a + b; }
    But remember, like chroot said, you cannot do this in C, you must be using C++; also did you notice you have two structure elements named "b"...?
  6. Jan 27, 2008 #5
    oops, sorry-heehee!

    but wow you can do that in c++, i'd better translate
  7. Jan 28, 2008 #6
    would you have to declare a and b in the meber function c?
  8. Jan 28, 2008 #7
  9. Jan 28, 2008 #8


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    No, a and b would be class variables (variously called 'member variables,' 'class fields,' 'member fields,' etc.).

    - Warren
  10. Jan 29, 2008 #9
    Hey guys I have a problem. When trying to compile this on dev c++ it wpon't let me, as it says I'm trying to make it a function, and when a change struct to a class, it says error before the class name, as well as calling 'private:' and 'public:' errors.
  11. Jan 29, 2008 #10
    in fact, my devc++ compiler does not seem to accept any c++ language-when i enter cout it says 'undeclared variable'. Do you guys know what the problem might be?
  12. Jan 29, 2008 #11


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Sounds like the problem is that you don't know C++.

    Show us some examples of the code that doesn't work and we'll try to help.

    - Warren
  13. Jan 24, 2012 #12
    Did you say #include <iostream>?

    If you put "using namespace std;" somewhere near the top of your file, does anything magic happen?
  14. Jan 24, 2012 #13


    User Avatar
    Homework Helper

    You mentioned "C", but you're trying to use a "C++" feature. If you want your source code to be compiled as "C++", the file name extension should be "cpp", not "c", such as "example.cpp" instead of "example.c".
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Mathematic operations and structures in C
  1. Ternary operator in C (Replies: 13)