100 likes | 120 Views
This example introduces the concept of structs and pointers in C programming. It demonstrates how to declare and use structs, as well as how to access struct members using pointers. The example includes functions to print book information and read student data from a file.
E N D
Introduction to Computer Organization & Systems COMP 21000 Topics: • Structs in C • Pointers and structs John Barr
Structs and Pointers #include <stdio.h> #include <string.h> struct Books { char title[50]; char author[50]; char subject[100]; intbook_id; }; Note that all the strings are allocated statically! Dynamic allocation makes life tricky! This example extends over 4 slides See /home/barr/Students/comp210/examples/structBook.c
Structs and Pointers /* 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 */
Structs and Pointers /* 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;
Structs and Pointers /* print Book1 info */ printBook( Book1 ); /* Print Book2 info */ printBook( Book2 ); return 0; } // end of main /* function printBook */ 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); }
#include <stdio.h> #include <stdlib.h> #include <stdio_ext.h> #define MAX 5 #define STRMAX 32 struct person{ char name[32]; int id; }; intreadline(char s[ ],int max) { intc,i=0; max--; while (i < max && (c = getchar()) != EOF && c != '\n') s[i++] =c; if (c != '\n') s[i++] = c; s[i] = '\0'; return(i); }
Structs and Pointers int main() { structperson *students; structperson *stdPtr; int i; FILE *sFile; students = (struct person*)malloc(MAX * sizeof(struct person)); stdPtr = students;
Structs and Pointers for (i = 0; i < MAX; i++) { printf("Enter name: "); readline(stdPtr->name, STRMAX); printf("Enter id: "); scanf("%d", &stdPtr->id); __fpurge(stdin); stdPtr++; } printf("\nStudents:\n"); stdPtr = students; for (i = 0; i < MAX; i++) { printf("%s", stdPtr->name); printf(", %d\n", stdPtr->id); stdPtr++; } }
fwritereads bytes and returns the number of bytes written Writing Structs Size of struct that you’re writing // “wb" means ”write bytes” sFile = fopen("students.txt", "wb"); int numWritten = fwrite(students, sizeof(struct person), 10, sFile); printf("Wrote %d bytes\n", numWritten); Pointer to memory to write, e.g., an array of struct File pointer Number of elements that you’re writing To open a file in a binary mode you must add a b to the end of the mode string; for example, "rb” for the reading and writing modes, you can add the b either after the plus sign - "r+b" - or before - "rb+” fwrite reference: https://www.tutorialspoint.com/c_standard_library/c_function_fwrite.htm
Reading Structs Size of struct that you’re reading // "rb" means "read bytes" sFile = fopen("students.txt", "rb"); intnumRead = fread(students, sizeof(struct person), 5, sFile); printf("Read %d elements\n", numRead); File pointer Pointer to memory to read into, e.g., an array of struct Number of elements to read fread returns the number of elements read fread reference: https://www.tutorialspoint.com/c_standard_library/c_function_fread.htm