Artikel Terkait Tutorial C
Structure is the collection of variables of different types under a single name for better visualisation of problem. Arrays is also collection of data but arrays can hold data of only one type whereas structure can hold data of one or more types.
When a structure is created, no memory is allocated. The structure definition is only the blueprint for the creating of variables. You can imagine it as a datatype. When you define an integer as below:
Here, a structure variable bill is defined which is of type structure
Tag : C++ Arrays, Multidimensional Arrays, C++ Function and Array, C++ String
How to define a structure in C++ programming?
Thestruct
keyword defines a structure type followed by an identifier(name of the structure). Then inside the curly braces, you can declare one or more members (declare variables inside curly braces) of that structure. For example:struct person {Here a structure person is defined which has three members: name, age and salary.
char name[50];
int age;
float salary;
};
When a structure is created, no memory is allocated. The structure definition is only the blueprint for the creating of variables. You can imagine it as a datatype. When you define an integer as below:
int foo;The
int
specifies that, variable foo can hold integer element only. Similarly, structure definition only specifies that, what property a structure variable holds when it is defined.How to define a structure variable?
Once you declare a structure person as above. You can define a structure variable as:person bill;
person
. When structure variable is defined, then only the required memory is allocated by the compiler. Considering you have either 32-bit or 64-bit system, the memory of float
is 4 bytes, memory of int
is 4 bytes and memory of char
is 1 byte. Hence, 58 bytes of memory is allocated for structure variable bill.How to access members of a structure?
The members of structure variable is accessed using dot operator. Suppose, you want to access age of structure variable bill and assign it 50 to it. You can perform this task by using following code below:bill.age = 50;
Example 1: C++ Structure
C++ Program to assign data to members of a structure variable and display it.#include <iostream>
using namespace std;
struct person {
char name[50];
int age;
float salary;
};
int main() {
person p1;
cout << "Enter Full name: ";
cin.get(p1.name, 50);
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;
cout << "\nDisplaying Information." << endl;
cout << "Name: " << p1.name << endl;
cout <<"Age: " << p1.age << endl;
cout << "Salary: " << p1.salary;
return 0;
}
OutputEnter Full name: Magdalena DankovaHere a structure person is declared which has three members. Inside
Enter age: 27
Enter salary: 1024.4
Displaying Information.
Name: Magdalena Dankova
Age: 27
Salary: 1024.4
main()
function, a structure variable p1 is defined. Then, the user is asked to enter information and data entered by user is displayed.Tag : C++ Arrays, Multidimensional Arrays, C++ Function and Array, C++ String
Selain Sebagai Penyedia Panduan Belajar Database dan Tutorial Pemrograman, Kami Juga Membagikan Kumpulan Source Code Program Aplikasi dan Ebook Pemrograman Terlengkap yang Bisa Anda Dapatkan Secara Gratis di Halaman :
Rekomendasi Web Hosting
- 20rb perbulan. Diskon hingga 40% kode kupon: MCP Daftar disini (apache).
- 10rb perbulan. Diskon hingga 75% kode kupon: MCP Daftar disini (litespeed).
- 10rb perbulan. Diskon hingga 70% kode kupon: aff-MCP Daftar disini (apache).