Differences Between the C and C++ Structures
C Structures |
C++ Structures |
---|---|
Only data members are allowed, it cannot have member functions. | Can hold both: member functions and data members. |
Cannot have static members. | Can have static members. |
Cannot have a constructor inside a structure. | Constructor creation is allowed. |
Direct Initialization of data members is not possible. | Direct Initialization of data members is possible. |
Writing the ‘struct’ keyword is necessary to declare structure-type variables. | Writing the ‘struct’ keyword is not necessary to declare structure-type variables. |
Do not have access modifiers. | Supports access modifiers. |
Only pointers to structs are allowed. | Can have both pointers and references to the struct. |
Sizeof operator will generate 0 for an empty structure. | Sizeof operator will generate 1 for an empty structure. |
Data Hiding is not possible. | Data Hiding is possible. |
Similarities Between the C and C++ Structures
Lets discuss some of the above mentioned differences and similarities one by one:
1. Member functions inside the structure: Structures in C cannot have member functions inside a structure but Structures in C++ can have member functions along with data members.
C
// C Program to Implement Member // functions inside structure #include <stdio.h> struct marks { int num; // Member function inside Structure to // take input and store it in "num" void Set( int temp) { num = temp; } // function used to display the values void display() { printf ( "%d" , num); } }; // Driver Program int main() { struct marks m1; // calling function inside Struct to // initialize value to num m1.Set(9); // calling function inside struct to // display value of Num m1.display(); } |
Output
This will generate an error in C but no error in C++.
C++
// C++ Program to Implement Member functions inside // structure #include <iostream> using namespace std; struct marks { int num; // Member function inside Structure to // take input and store it in "num" void Set( int temp) { num = temp; } // function used to display the values void display() { cout << "num=" << num; } }; // Driver Program int main() { marks m1; // calling function inside Struct to // initialize value to num m1.Set(9); // calling function inside struct to // display value of Num m1.display(); } |
num=9
2. Static Members: C structures cannot have static members but are allowed in C++.
C
// C program with structure static member struct Record { static int x; }; // Driver program int main() { return 0; } |
C++
// C++ program with structure static member struct Record { static int x; }; // Driver program int main() { return 0; } |
This will generate an error in C but not in C++.
3. Constructor creation in structure: Structures in C cannot have a constructor inside a structure but Structures in C++ can have Constructor creation.
C
// C program to demonstrate that // Constructor is not allowed #include <stdio.h> struct Student { int roll; Student( int x) { roll = x; } }; // Driver Program int main() { struct Student s(2); printf ( "%d" , s.x); return 0; } |
C++
// CPP program to initialize data member in c++ #include <iostream> using namespace std; struct Student { int roll; Student( int x) { roll = x; } }; // Driver Program int main() { struct Student s(2); cout << s.roll; return 0; } |
This will generate an error in C.
Output in C++:
2
4. Direct Initialization: We cannot directly initialize structure data members in C but we can do it in C++.
C
// C program to demonstrate that direct // member initialization is not possible in C #include <stdio.h> struct Record { int x = 7; }; // Driver Program int main() { struct Record s; printf ( "%d" , s.x); return 0; } |
C++
// CPP program to initialize data member in c++ #include <iostream> using namespace std; struct Record { int x = 7; }; // Driver Program int main() { Record s; cout << s.x << endl; return 0; } |
This will generate an error in C.
Output in C++:
7
5. Using struct keyword: In C, we need to use a struct to declare a struct variable. In C++, a struct is not necessary. For example, let there be a structure for Record. In C, we must use “struct Record” for Record variables. In C++, we need not use struct, and using ‘Record‘ only would work.
6. Access Modifiers: C structures do not have access modifiers as these modifiers are not supported by the language. C++ structures can have this concept as it is inbuilt in the language.
7. Pointers and References: In C++, there can be both pointers and references to a struct in C++, but only pointers to structs are allowed in C.
8. sizeof operator: This operator will generate 0 for an empty structure in C whereas 1 for an empty structure in C++.
C
// C program to illustrate empty structure #include <stdio.h> // empty structure struct Record { }; // Driver Code int main() { struct Record s; printf ( "%lu\n" , sizeof (s)); return 0; } |
C++
// C++ program to illustrate empty structure #include <iostream> using namespace std; // empty structure struct Record { }; // Driver program int main() { struct Record s; cout << sizeof (s); return 0; } // This code is contributed by Shubham Sharma |
Output in C:
0
Output in C++:
1
NOTE: The default type of sizeof is long unsigned int , that’s why “%lu” is used instead of “%d” in printf function.
9. Data Hiding: C structures do not allow the concept of Data hiding but are permitted in C++ as it is an object-oriented language whereas C is not.
10. Constant Members: C struct may allow to declare constant members, but no way to initialize. But in C++, you can initialize using constructor initializer list
C
#include <stdio.h> struct C_struct { const int i; int k; }; int main() { printf ( "Struct with constant members, but how to init??" ); return 0; } |
C++
#include <iostream> using namespace std; struct Cpp_Struct { public : const int i; int k; Cpp_Struct():i(2),k(3){} }; int main() { Cpp_Struct obj1; cout << "Struct with constant members: " << obj1.i << " " << obj1.k << endl; return 0; } |
Output in C:
Struct with constant members, but how to init??
Output in C++:
Struct with constant members: 2 3
Related Article: Structure vs Class in C++
Difference Between C Structures and C++ Structures
Let’s discuss, what are the differences between structures in C and structures in C++? In C++, structures are similar to classes.