Manufaktur industri
Industri Internet of Things | bahan industri | Pemeliharaan dan Perbaikan Peralatan | Pemrograman industri |
home  MfgRobots >> Manufaktur industri >  >> Industrial programming >> Bahasa C

Struktur Data C++

Array C/C++ memungkinkan Anda untuk mendefinisikan variabel yang menggabungkan beberapa item data dari jenis yang sama, tetapi struktur adalah tipe data lain yang ditentukan pengguna yang memungkinkan Anda untuk menggabungkan item data dari berbagai jenis.

Struktur digunakan untuk mewakili catatan, misalkan Anda ingin melacak buku-buku Anda di perpustakaan. Anda mungkin ingin melacak atribut berikut tentang setiap buku −

Mendefinisikan Struktur

Untuk mendefinisikan struktur, Anda harus menggunakan pernyataan struct. Pernyataan struct mendefinisikan tipe data baru, dengan lebih dari satu anggota, untuk program Anda. Format pernyataan struct adalah ini

struct [structure tag] {
   member definition;
   member definition;
   ...
   member definition;
} [one or more structure variables];  

tag struktur adalah opsional dan setiap definisi anggota adalah definisi variabel normal, seperti int i; atau mengambang f; atau definisi variabel valid lainnya. Di akhir definisi struktur, sebelum titik koma terakhir, Anda dapat menentukan satu atau lebih variabel struktur tetapi ini opsional. Inilah cara Anda mendeklarasikan struktur Buku −

struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} book;  

Mengakses Anggota Struktur

Untuk mengakses setiap anggota struktur, kami menggunakan operator akses anggota (.) . Operator akses anggota dikodekan sebagai titik antara nama variabel struktur dan anggota struktur yang ingin kita akses. Anda akan menggunakan struct kata kunci untuk mendefinisikan variabel tipe struktur. Berikut ini adalah contoh untuk menjelaskan penggunaan struktur

Demo Langsung
#include <iostream>
#include <cstring>
 
using namespace std;
 
struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
int main() {
   struct Books Book1;        // Declare Book1 of type Book
   struct Books Book2;        // Declare Book2 of type Book
 
   // book 1 specification
   strcpy( Book1.title, "Learn C++ Programming");
   strcpy( Book1.author, "Chand Miyan"); 
   strcpy( Book1.subject, "C++ Programming");
   Book1.book_id = 6495407;

   // book 2 specification
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Yakit Singha");
   strcpy( Book2.subject, "Telecom");
   Book2.book_id = 6495700;
 
   // Print Book1 info
   cout << "Book 1 title : " << Book1.title <<endl;
   cout << "Book 1 author : " << Book1.author <<endl;
   cout << "Book 1 subject : " << Book1.subject <<endl;
   cout << "Book 1 id : " << Book1.book_id <<endl;

   // Print Book2 info
   cout << "Book 2 title : " << Book2.title <<endl;
   cout << "Book 2 author : " << Book2.author <<endl;
   cout << "Book 2 subject : " << Book2.subject <<endl;
   cout << "Book 2 id : " << Book2.book_id <<endl;

   return 0;
}

Ketika kode di atas dikompilasi dan dieksekusi, menghasilkan hasil sebagai berikut

Book 1 title : Learn C++ Programming
Book 1 author : Chand Miyan
Book 1 subject : C++ Programming
Book 1 id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Yakit Singha
Book 2 subject : Telecom
Book 2 id : 6495700

Struktur sebagai Argumen Fungsi

Anda dapat meneruskan struktur sebagai argumen fungsi dengan cara yang sangat mirip saat Anda meneruskan variabel atau penunjuk lainnya. Anda akan mengakses variabel struktur dengan cara yang sama seperti yang Anda akses pada contoh di atas −

Demo Langsung
#include <iostream>
#include <cstring>
 
using namespace std;
void printBook( struct Books book );

struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
int main() {
   struct Books Book1;        // Declare Book1 of type Book
   struct Books Book2;        // Declare Book2 of type Book
 
   // book 1 specification
   strcpy( Book1.title, "Learn C++ Programming");
   strcpy( Book1.author, "Chand Miyan"); 
   strcpy( Book1.subject, "C++ Programming");
   Book1.book_id = 6495407;

   // book 2 specification
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Yakit Singha");
   strcpy( Book2.subject, "Telecom");
   Book2.book_id = 6495700;
 
   // Print Book1 info
   printBook( Book1 );

   // Print Book2 info
   printBook( Book2 );

   return 0;
}
void printBook( struct Books book ) {
   cout << "Book title : " << book.title <<endl;
   cout << "Book author : " << book.author <<endl;
   cout << "Book subject : " << book.subject <<endl;
   cout << "Book id : " << book.book_id <<endl;
}

Ketika kode di atas dikompilasi dan dieksekusi, menghasilkan hasil sebagai berikut

Book title : Learn C++ Programming
Book author : Chand Miyan
Book subject : C++ Programming
Book id : 6495407
Book title : Telecom Billing
Book author : Yakit Singha
Book subject : Telecom
Book id : 6495700

Penunjuk ke Struktur

Anda dapat mendefinisikan pointer ke struktur dengan cara yang sangat mirip seperti Anda mendefinisikan pointer ke variabel lain sebagai berikut −

struct Books *struct_pointer;

Sekarang, Anda dapat menyimpan alamat variabel struktur dalam variabel penunjuk yang ditentukan di atas. Untuk mencari alamat dari variabel struktur, tempatkan operator &sebelum nama struktur sebagai berikut −

struct_pointer = &Book1;

Untuk mengakses anggota struktur menggunakan pointer ke struktur itu, Anda harus menggunakan operator -> sebagai berikut −

struct_pointer->title;

Mari kita tulis ulang contoh di atas dengan menggunakan penunjuk struktur, semoga mudah untuk memahami konsepnya

Demo Langsung
#include <iostream>
#include <cstring>
 
using namespace std;
void printBook( struct Books *book );

struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
int main() {
   struct Books Book1;        // Declare Book1 of type Book
   struct Books Book2;        // Declare Book2 of type Book
 
   // Book 1 specification
   strcpy( Book1.title, "Learn C++ Programming");
   strcpy( Book1.author, "Chand Miyan"); 
   strcpy( Book1.subject, "C++ Programming");
   Book1.book_id = 6495407;

   // Book 2 specification
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Yakit Singha");
   strcpy( Book2.subject, "Telecom");
   Book2.book_id = 6495700;
 
   // Print Book1 info, passing address of structure
   printBook( &Book1 );

   // Print Book1 info, passing address of structure
   printBook( &Book2 );

   return 0;
}

// This function accept pointer to structure as parameter.
void printBook( struct Books *book ) {
   cout << "Book title : " << book->title <<endl;
   cout << "Book author : " << book->author <<endl;
   cout << "Book subject : " << book->subject <<endl;
   cout << "Book id : " << book->book_id <<endl;
}

Ketika kode di atas dikompilasi dan dieksekusi, menghasilkan hasil sebagai berikut

Book title : Learn C++ Programming
Book author : Chand Miyan
Book subject : C++ Programming
Book id : 6495407
Book title : Telecom Billing
Book author : Yakit Singha
Book subject : Telecom
Book id : 6495700

Kata Kunci typedef

Ada cara yang lebih mudah untuk mendefinisikan struct atau Anda bisa "alias" jenis yang Anda buat. Misalnya

typedef struct {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} Books;

Sekarang, Anda dapat menggunakan Buku langsung untuk mendefinisikan variabel Buku ketik tanpa menggunakan kata kunci struct. Berikut adalah contohnya

Books Book1, Book2;

Anda dapat menggunakan typedef kata kunci untuk non-struct serta sebagai berikut

typedef long int *pint32;
 
pint32 x, y, z;

x, y dan z semuanya adalah pointer ke int panjang.


Bahasa C

  1. Tipe Data C++
  2. Operator C++
  3. Komentar C++
  4. Tipe Data C++ Char dengan Contoh
  5. Teknologi Penyimpanan Data Magnetik Generasi Berikutnya
  6. Java - Struktur Data
  7. C - Struktur
  8. C - typedef
  9. Struktur Data C++
  10. Abstraksi Data dalam C++