Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to detect if the value of a struct field has changed in C?

so lets say I have a function which updates a struct field:

struct person {
    int age;
};

void update_struct (int value) {
    person->age = value;
}

I want to detect whether the value of the struct field has changed in another function.

void another_function () {
    
    if (there is a change in the value of the struct field 'age') {
        // do the following;
    }

}

I am struggling to write an if statement condition for that. Help would be much appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can’t do this directly, either you keep a flag which you modify when you alter a field or you keep a copy of the struct and memcmp it.

The latter solution wastes more memory but it has the advantage that it’s blindly working unless you have pointers inside your struct.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading