Mathematic operations and structures in C

Click For Summary

Discussion Overview

The discussion revolves around the implementation of mathematical operations and structures in C and C++. Participants explore how to manage structure members and perform calculations within those structures, particularly focusing on the assignment of one member based on the values of others.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant inquires about how to make a structure member equal to the sum of other members for all instances of that structure.
  • Another participant clarifies that a global change is not possible and suggests performing the assignment for each instance individually.
  • There is a suggestion to convert the structure to a class in C++ and use a member function to calculate the value of one member based on others.
  • Some participants note the presence of duplicate member names in the structure definition, prompting a correction.
  • A participant expresses difficulty in compiling C++ code, indicating issues with the compiler and syntax errors related to class usage.
  • There are questions about the necessity of declaring member variables within member functions in C++.
  • Another participant emphasizes the need for the correct file extension to compile C++ code.

Areas of Agreement / Disagreement

Participants generally agree that the original structure cannot perform the desired operation in C and that using C++ features is necessary for the proposed solution. However, there is disagreement regarding the participant's understanding of C++ and the specific errors encountered during compilation.

Contextual Notes

Some limitations include the confusion between C and C++ features, the need for proper syntax and file extensions for C++ compilation, and the unresolved issues regarding the participant's compiler setup.

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 count 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 count 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".
 

Similar threads

Replies
12
Views
2K
  • · Replies 52 ·
2
Replies
52
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 66 ·
3
Replies
66
Views
6K
Replies
10
Views
2K
Replies
6
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K