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

C - Strukturen

Arrays ermöglichen die Definition von Variablentypen, die mehrere Datenelemente derselben Art enthalten können. Ähnlich Struktur ist ein weiterer benutzerdefinierter Datentyp, der in C verfügbar ist und es erlaubt, Datenelemente verschiedener Art zu kombinieren.

Strukturen werden verwendet, um einen Datensatz darzustellen. Angenommen, Sie möchten Ihre Bücher in einer Bibliothek verfolgen. Vielleicht möchten Sie die folgenden Attribute zu jedem Buch nachverfolgen −

Eine Struktur definieren

Um eine Struktur zu definieren, müssen Sie struct verwenden Aussage. Die Struct-Anweisung definiert einen neuen Datentyp mit mehr als einem Mitglied. Das Format der Struct-Anweisung ist wie folgt:−

struct [structure tag] {

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

Das Struktur-Tag ist optional und jede Mitgliedsdefinition ist eine normale Variablendefinition, wie z. B. int i; oder Schwimmer f; oder jede andere gültige Variablendefinition. Am Ende der Strukturdefinition, vor dem abschließenden Semikolon, können Sie eine oder mehrere Strukturvariablen angeben, dies ist jedoch optional. So würden Sie die Book-Struktur deklarieren −

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

Auf Strukturmitglieder zugreifen

Um auf ein beliebiges Mitglied einer Struktur zuzugreifen, verwenden wir den Member-Zugriffsoperator (.) . Der Elementzugriffsoperator wird als Punkt zwischen dem Strukturvariablennamen und dem Strukturelement, auf das wir zugreifen möchten, codiert. Sie würden das Schlüsselwort struct verwenden um Variablen vom Strukturtyp zu definieren. Das folgende Beispiel zeigt, wie man eine Struktur in einem Programm verwendet −

Live-Demo
#include <stdio.h>
#include <string.h>
 
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, "C Programming");
   strcpy( Book1.author, "Nuha Ali"); 
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;

   /* book 2 specification */
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");
   Book2.book_id = 6495700;
 
   /* print Book1 info */
   printf( "Book 1 title : %s\n", Book1.title);
   printf( "Book 1 author : %s\n", Book1.author);
   printf( "Book 1 subject : %s\n", Book1.subject);
   printf( "Book 1 book_id : %d\n", Book1.book_id);

   /* print Book2 info */
   printf( "Book 2 title : %s\n", Book2.title);
   printf( "Book 2 author : %s\n", Book2.author);
   printf( "Book 2 subject : %s\n", Book2.subject);
   printf( "Book 2 book_id : %d\n", Book2.book_id);

   return 0;
}

Wenn der obige Code kompiliert und ausgeführt wird, erzeugt er das folgende Ergebnis −

Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700

Strukturen als Funktionsargumente

Sie können eine Struktur genauso als Funktionsargument übergeben wie jede andere Variable oder jeden anderen Zeiger.

Live-Demo
#include <stdio.h>
#include <string.h>
 
struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};

/* function declaration */
void printBook( struct Books book );

int main( ) {

   struct Books Book1;        /* Declare Book1 of type Book */
   struct Books Book2;        /* Declare Book2 of type Book */
 
   /* book 1 specification */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali"); 
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;

   /* book 2 specification */
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");
   Book2.book_id = 6495700;
 
   /* print Book1 info */
   printBook( Book1 );

   /* Print Book2 info */
   printBook( Book2 );

   return 0;
}

void printBook( struct Books book ) {

   printf( "Book title : %s\n", book.title);
   printf( "Book author : %s\n", book.author);
   printf( "Book subject : %s\n", book.subject);
   printf( "Book book_id : %d\n", book.book_id);
}

Wenn der obige Code kompiliert und ausgeführt wird, erzeugt er das folgende Ergebnis −

Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407
Book title : Telecom Billing
Book author : Zara Ali
Book subject : Telecom Billing Tutorial
Book book_id : 6495700

Zeiger auf Strukturen

Sie können Zeiger auf Strukturen genauso definieren wie Zeiger auf jede andere Variable −

struct Books *struct_pointer;

Nun können Sie die Adresse einer Strukturvariablen in der oben definierten Pointer-Variablen speichern. Um die Adresse einer Strukturvariablen zu finden, setzen Sie das '&'; Operator vor dem Namen der Struktur wie folgt:−

struct_pointer = &Book1;

Um auf die Mitglieder einer Struktur mit einem Zeiger auf diese Struktur zuzugreifen, müssen Sie den →-Operator wie folgt verwenden −

struct_pointer->title;

Lassen Sie uns das obige Beispiel mit dem Strukturzeiger umschreiben.

Live-Demo
#include <stdio.h>
#include <string.h>
 
struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};

/* function declaration */
void printBook( struct Books *book );
int main( ) {

   struct Books Book1;        /* Declare Book1 of type Book */
   struct Books Book2;        /* Declare Book2 of type Book */
 
   /* book 1 specification */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali"); 
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;

   /* book 2 specification */
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");
   Book2.book_id = 6495700;
 
   /* print Book1 info by passing address of Book1 */
   printBook( &Book1 );

   /* print Book2 info by passing address of Book2 */
   printBook( &Book2 );

   return 0;
}

void printBook( struct Books *book ) {

   printf( "Book title : %s\n", book->title);
   printf( "Book author : %s\n", book->author);
   printf( "Book subject : %s\n", book->subject);
   printf( "Book book_id : %d\n", book->book_id);
}

Wenn der obige Code kompiliert und ausgeführt wird, erzeugt er das folgende Ergebnis −

Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407
Book title : Telecom Billing
Book author : Zara Ali
Book subject : Telecom Billing Tutorial
Book book_id : 6495700

Bitfelder

Bitfelder ermöglichen das Packen von Daten in eine Struktur. Dies ist besonders nützlich, wenn Arbeitsspeicher oder Datenspeicher knapp sind. Typische Beispiele sind −

C erlaubt uns, dies in einer Strukturdefinition zu tun, indem wir :bit length nach der Variablen setzen. Zum Beispiel −

struct packed_struct {
   unsigned int f1:1;
   unsigned int f2:1;
   unsigned int f3:1;
   unsigned int f4:1;
   unsigned int type:4;
   unsigned int my_int:9;
} pack;

Hier enthältpacked_struct 6 Mitglieder:Vier 1-Bit-Flags f1..f3, einen 4-Bit-Typ und ein 9-Bit-my_int.

C packt die obigen Bitfelder automatisch so kompakt wie möglich, vorausgesetzt, dass die maximale Länge des Felds kleiner oder gleich der ganzzahligen Wortlänge des Computers ist. Wenn dies nicht der Fall ist, können einige Compiler eine Speicherüberlappung für die Felder zulassen, während andere das nächste Feld im nächsten Wort speichern würden.


C Sprache

  1. Strukturen und Klassen in C++
  2. Semaphoren:Versorgungsdienste und Datenstrukturen
  3. Buchbesprechung:Digitalisieren oder sterben
  4. Eine sehr menschliche Zukunft
  5. Magnetische Datenspeichertechnologie der nächsten Generation
  6. 17 beste Programmierbücher für 2021
  7. Java - Datenstrukturen
  8. C - Typdef
  9. C++-Datenstrukturen
  10. C# - Strukturen