
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Operations on struct variables in C
Here we will see what type of operations can be performed on struct variables. Here basically one operation can be performed for struct. The operation is assignment operation. Some other operations like equality check or other are not available for stack.
Example
#include <stdio.h> typedef struct { //define a structure for complex objects int real, imag; }complex; void displayComplex(complex c){ printf("(%d + %di)\n", c.real, c.imag); } main() { complex c1 = {5, 2}; complex c2 = {8, 6}; printf("Complex numbers are:\n"); displayComplex(c1); displayComplex(c2); }
Output
Complex numbers are: (5 + 2i) (8 + 6i)
This works fine as we have assigned some values into struct. Now if we want to compare two struct objects, let us see the difference.
Example
#include <stdio.h> typedef struct { //define a structure for complex objects int real, imag; }complex; void displayComplex(complex c){ printf("(%d + %di)\n", c.real, c.imag); } main() { complex c1 = {5, 2}; complex c2 = c1; printf("Complex numbers are:\n"); displayComplex(c1); displayComplex(c2); if(c1 == c2){ printf("Complex numbers are same."); } else { printf("Complex numbers are not same."); } }
Output
[Error] invalid operands to binary == (have 'complex' and 'complex')
- Related Articles
- UInt16 Struct in C#
- UInt32 Struct in C#
- UInt64 Struct in C#
- Decimal Struct in C#
- Char Struct in C#
- Byte Struct in C#
- C# Int16 Struct
- C# Int32 Struct
- Int 64 Struct in C#
- Difference between 'struct' and 'typedef struct' in C++?
- C/C++ Struct vs Class
- Difference between 'struct' and 'typedef struct' in C++ program?
- Find max in struct array using C++.
- C++ to perform certain operations on a sequence
- Deep Copy of Struct Member Arrays in C

Advertisements