Industrielle Fertigung
Industrielles Internet der Dinge | Industrielle Materialien | Gerätewartung und Reparatur | Industrielle Programmierung |
home  MfgRobots >> Industrielle Fertigung >  >> Industrial programming >> C Sprache

C++ Datenstrukturen: Arrays, Structs & mehr für effiziente Programme

C/C++-Arrays fassen gleichartige Datenitems zusammen. Strukturen (struct) sind benutzerdefinierte Typen, die verschiedenartige Datenitems kombinieren – ideal für Records.

Beispiel: Buchverwaltung in einer Bibliothek mit Attributen Titel, Autor, Thema, Buch-ID.

Struktur definieren

struct-Statement definiert neuen Datentyp mit mehreren Membern:

struct [struktur_tag] {
    member_definition;
    member_definition;
    ...
} [struktur_variablen];

struktur_tag optional. Memberdefinitionen wie normale Variablen (int i;, float f;...). Am Ende vor Semikolon optional Variablen deklarieren.

Beispiel Books:

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

Zugriff auf Struktur-Member

Mit Member-Zugriffsoperator (.) – Punkt zwischen Variablenname und Membername.

#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, Book2;
    strcpy(Book1.title, "Learn C++ Programming");
    strcpy(Book1.author, "Chand Miyan");
    strcpy(Book1.subject, "C++ Programming");
    Book1.book_id = 6495407;
    strcpy(Book2.title, "Telecom Billing");
    strcpy(Book2.author, "Yakit Singha");
    strcpy(Book2.subject, "Telecom");
    Book2.book_id = 6495700;
    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;
    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;
}

Strukturen als Funktionsargumente

Übergabe wie jede Variable/Pointer. Zugriff im Funktionskörper identisch.

void printBook(struct Books book);  // Deklaration
// ... Definition wie oben ...
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;
}

Pointer auf Strukturen

Definition: struct Books *struct_pointer;

Adresse speichern: struct_pointer = &Book1;

Member-Zugriff via Pointer: struct_pointer->title; (Pfeiloperator).

void printBook(struct Books* book);  // Pointer-Parameter
// ... in main: printBook(&Book1); ...
void printBook(struct Books* book) {
    cout << "Book title: " << book->title << endl;
    // ... book->author etc.
}

Das typedef-Schlüsselwort

Einfacheres Definieren / Aliase für Typen:

typedef struct {
    char title[50];
    char author[50];
    char subject[100];
    int book_id;
} Books;
// Jetzt ohne struct-Keyword nutzbar:
Books Book1, Book2;

Auch für Nicht-Structs:

typedef long int* pint32;
pint32 x, y, z;  // alle Pointer auf long int

C Sprache

  1. C++-Datentypen verstehen: Grundlagen, Modifizierer und Best Practices
  2. C++-Operatoren meistern: Ein vollständiger Leitfaden mit praktischen Beispielen
  3. C++-Kommentare: Best Practices für lesbaren, wartbaren Code
  4. C++ char‑Datentyp meistern: Deklaration, ASCII & Konvertierungstechniken
  5. Datenspeicher-Revolution: Nächstgen-Magnetnanostrukturen mit Unsichtbarkeitsumhang
  6. Java Datenstrukturen: Kernklassen & Collections Framework erkunden
  7. C-Strukturen meistern: Komplexe Daten effizient organisieren
  8. C typedef erklärt: Code vereinfachen mit benutzerdefinierten Typnamen
  9. C++ Datenstrukturen: Arrays, Structs & mehr für effiziente Programme
  10. Datenabstraktion in C++: Interface-Implementierung-Trennung