1 / 10

Introduction to Computer Organization & Systems

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.

earlyd
Download Presentation

Introduction to Computer Organization & Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Computer Organization & Systems COMP 21000 Topics: • Structs in C • Pointers and structs John Barr

  2. 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

  3. 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 */

  4. 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;

  5. 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); }

  6. #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); }

  7. Structs and Pointers int main() { structperson *students; structperson *stdPtr; int i; FILE *sFile; students = (struct person*)malloc(MAX * sizeof(struct person)); stdPtr = students;

  8. 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++; } }

  9. 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

  10. 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

More Related