Mathematic operations and structures in C

AI Thread Summary
To make the integer variable 'c' in a struct equal to the sum of 'a' and 'b' for all instances of that struct, it is necessary to perform the assignment for each instance individually, as there is no way to implement a global change. In C++, a more efficient approach is to convert the struct into a class and use a member function to calculate 'c' dynamically, rather than storing it as a separate variable. The suggested class structure includes a method that returns the sum of 'a' and 'b'. However, users must ensure they are using proper C++ syntax, including including the necessary headers like <iostream> and using the correct file extension (.cpp) for C++ code. Issues with compilation often arise from not properly setting up the environment or misunderstanding the language features.
lewis198
Messages
95
Reaction score
0
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.
 
Technology news on Phys.org
*variables rather than variable s
 
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
 
chroot said:
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.

If you want to do that it would look like:

Code:
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"...?
 
oops, sorry-heehee!

but wow you can do that in c++, i'd better translate
 
would you have to declare a and b in the meber function c?
 
*member
 
No, a and b would be class variables (variously called 'member variables,' 'class fields,' 'member fields,' etc.).

- Warren
 
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.
 
  • #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?
 
  • #11
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
 
  • #12
lewis198 said:
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?

Did you say #include <iostream>?

If you put "using namespace std;" somewhere near the top of your file, does anything magic happen?
 
  • #13
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".
 
Back
Top