/
Introduction to Computer Organization & Systems Introduction to Computer Organization & Systems

Introduction to Computer Organization & Systems - PowerPoint Presentation

debby-jeon
debby-jeon . @debby-jeon
Follow
342 views
Uploaded On 2020-01-09

Introduction to Computer Organization & Systems - PPT Presentation

Introduction to Computer Organization amp Systems Topics Structs in C Pointers and structs COMP 21000 John Barr Structs and Pointers include lt stdioh gt include lt stringh gt struct ID: 772353

struct book structs printf book struct printf structs int students stdptr book1 book2 max pointers person strcpy amp subject

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Introduction to Computer Organization &a..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

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

Structs and Pointers #include <stdio.h>#include <string.h> struct Books { char title[50]; char author[50]; char subject[100]; int book_id;};2-2 See /home/ barr /Students/comp210/examples/ structBook.c Note that all the strings are allocated statically! Dynamic allocation makes life tricky! This example extends over 4 slides

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

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

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

#include <stdio.h >#include <stdlib.h>#include <stdio_ext.h>#define MAX 5#define STRMAX 32 struct person{ char name[32]; int id; }; int readline(char s[ ],int max){ int c,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(){ struct person *students; struct person * stdPtr ; int i; FILE *sFile; students = ( struct person*) malloc (MAX * sizeof ( struct person)); stdPtr = students; 2- 7

Structs and Pointers 2- 8 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 ++; } }

Writing Structs // “ wb" means ”write bytes” sFile = fopen("students.txt", "wb"); int numWritten = fwrite(students, sizeof(struct person), 10, sFile); printf("Wrote %d bytes\n", numWritten); 2- 9 Number of elements that you’re writing Size of struct that you’re writing File pointer Pointer to memory to write, e.g., an array of struct fwrite reads bytes and returns the number of bytes written fwrite reference: https:// www.tutorialspoint.com / c_standard_library / c_function_fwrite.htm 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 +”

Reading Structs// " rb" means "read bytes" sFile = fopen ("students.txt", "rb"); int numRead = fread (students, sizeof(struct person), 5, sFile ); printf ("Read % d elements\ n", numRead ); 2- 10 Number of elements to read Size of struct that you’re reading File pointer Pointer to memory to read into, e.g., an array of struct fread returns the number of elements read fread reference: https:// www.tutorialspoint.com / c_standard_library / c_function_fread.htm