L10_struct.pdf - Programmeringsteknik DT143G Lecture Notes PDF

Document Details

SaintlySphene5524

Uploaded by SaintlySphene5524

Örebro University

2024

DT143G

Pascal Rebreyend

Tags

programming struct c programming data structures programming

Summary

This document is a lecture presentation on data structures in a programming course, likely related to C programming at Örebro University. The document focuses on structures in C and details how to implement lists and arrays using structures and functions.

Full Transcript

Introduction Programmeringsteknik DT143G Lecture 10: Struct and pointers Pascal Rebreyend 05-12-2024 Pascal Rebreyend Programmeringsteknik DT143G Introduction Today’s contents Struct Defining our own struct Why stru...

Introduction Programmeringsteknik DT143G Lecture 10: Struct and pointers Pascal Rebreyend 05-12-2024 Pascal Rebreyend Programmeringsteknik DT143G Introduction Today’s contents Struct Defining our own struct Why struct are important! Bit fields In short, complex, heterogenous data-structures Connection with pointers Pascal Rebreyend Programmeringsteknik DT143G Introduction Combining different datatypes Single datatype: One piece of information Arrays: A collection of same datatype Struct (Structure): A collection of different datatypes #include int main() { struct Person{ char name; int age; char gender; char address; }; struct Person student1; student1.age=32; sprintf(student1.name,"John"); printf("%s is %d\n",student1.name,student1.age); } Pascal Rebreyend Programmeringsteknik DT143G Introduction Usages Very convenient for database-like software You can have an array of structs! BUT: one (or more) field can be a pointer! And a pointer to a struct or array of structs! Means we can create a data-structure as complex as we want! Pascal Rebreyend Programmeringsteknik DT143G Introduction Typedef and functions #include typedef struct Persons { char name; int age; char gender; char address; } Person; void display(Person p) { printf("%s is %d\n",p.name,p.age); } Person copypandmod(Person p) { Person ret; ret=p; ret.age=18; return(ret); } Pascal Rebreyend Programmeringsteknik DT143G Introduction Typedef and functions int main() { Person student1,student2,s3; student1.age=50; sprintf(student1.name,"John"); display(student1); student2=student1; student2.age=25; display(student2); s3=copypandmod(student1); display(s3); } Pascal Rebreyend Programmeringsteknik DT143G Introduction Remarks typedef: We create a new datatype Convenient to pass and return from functions Particular syntax: the dot to access the different fields. Pascal Rebreyend Programmeringsteknik DT143G Introduction struct and dynamic arrays #include #include #define N 10 typedef struct Persons { char name; int age; char gender; char address; } Person; void display(Person p) { printf("%s is %d\n",p.name,p.age); } Pascal Rebreyend Programmeringsteknik DT143G Introduction struct and dynamic arrays int main() { Person *p,*ind; p=calloc(N,sizeof(Person)); ind=malloc(sizeof(Person)); printf("Size of the struct: %lu\n",sizeof(Person)); sprintf(ind->name,"Bob"); ind->age=45; sprintf(p.name,"John"); p.age=23; display(*ind); display(p); free(p); free(ind); return 0; } Pascal Rebreyend Programmeringsteknik DT143G Introduction Bit fields #include struct test { unsigned int flag1:1; unsigned int dayofweek:3; int value:12; }; int main() { struct test v; v.flag1=1; v.dayofweek=5; printf("sizeof: %lu\n",sizeof(struct test)); printf("Values: %u %u\n",v.flag1,v.dayofweek); return 0; } Pascal Rebreyend Programmeringsteknik DT143G Introduction Bit Fields We can just use a number of bit needed Useful for binary flags One example of features not often used Pascal Rebreyend Programmeringsteknik DT143G Introduction Example: List A list: A set of elements, each of them pointing to the next one. Or nothing One member of a struct can be a pointer (to another struct element) A specific value for pointers: NULL Flexibility (easy to insert or remove one element) Can be generalize to more complex things like graphs,... A very important and commonly used data-structure Pascal Rebreyend Programmeringsteknik DT143G Introduction Example: List #include #include typedef struct elem { int value; struct elem *next; } Elem; Elem *list_add(Elem *l,int v) { Elem *new,*ret; new=malloc(sizeof(Elem)); new->next=NULL; new->value=v; ret=l; if (l==NULL) { ret=new; } else { while (l->next!=NULL) { l=l->next; } l->next=new; } return(ret); } Pascal Rebreyend Programmeringsteknik DT143G Introduction Example: A list void display_list(Elem *e) { while (e!=NULL) { printf("%d ",e->value); e=e->next; } } void main() { Elem *list=NULL; int a; scanf("%d",&a); while (a!=0) { list=list_add(list,a); scanf("%d",&a); } display_list(list); } Pascal Rebreyend Programmeringsteknik DT143G Introduction List and their usages Only one pointer (to the first element of the list) One mistake: You loose all or some element (+ errors... ) We can insert, remove,... elements Here single-linked list. Can also be double-linked (pointer to previous and next element) Pascal Rebreyend Programmeringsteknik DT143G Introduction Conclusion Really practical to group into one variable different type of information Next session: We will see the power combined with pointers Union exists too (not covered in this course) Union and bit fields: How the programmer can be low-level. Pascal Rebreyend Programmeringsteknik DT143G

Use Quizgecko on...
Browser
Browser