

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth exploration of structures and pointers in C programming. It covers the basics of structures, including their declaration, members, and copying. The document also delves into the use of pointers with structures, accessing members using the dot operator, and offsetof. Additionally, it discusses self-referential structures and their corrections, incomplete declarations, and structures as function arguments. The document concludes with an explanation of linked lists and their operations.
Typology: Summaries
1 / 25
This page cannot be seen from the preview
Don't miss anything!


















Declares x to be a structure having two members, a and b. In addition, the structure tag S is created for use in future declarations. Omitting the tag field; cannot create any more variables with the same type as z Incomplete declaration which informs the compiler that S is a structure tag to be defined later Omitting the member list declares another structure variable y with the same type as x Omitting the variable list defines the tag S for use in later declarations
Typedefs è^ typedef
So è^ *Simple x; Simple y[20], z; Now x, y, and z are all the same TYPE. Similar to è^ *int x; int y[20], z;
#include<stdio.h> typedef struct { char *name; int number; } TELEPHONE; int main() { TELEPHONE index; TELEPHONE *ptr_myindex; ptr_myindex = &index; ptr_myindex->name = "Jane Doe"; ptr_myindex->number = 12345; printf("Name: %s\n", ptr_myindex->name); printf("Telephone number: %d\n", ptr_myindex->number); return 0; } What is going on here? Remember: TELEPHONE is a type of structure;
#include<stdio.h> #include <stdlib.h> typedef struct rec { int i; float PI; char A; } RECORD; int main() { RECORD ptr_one; ptr_one = (RECORD ) malloc (sizeof(RECORD)); (ptr_one).i = 10; (ptr_one).PI = 3.14; (ptr_one).A = 'a'; printf("First value: %d\n",(ptr_one).i); printf("Second value: %f\n", (ptr_one).PI); printf("Third value: %c\n", (ptr_one).A); free(ptr_one); return 0; } struct rec *ptr_one; ptr_one =(struct rec *) malloc (sizeof(struct rec)); ptr_one->i = 10; ptr_one->PI = 3.14; ptr_one->A = 'a'; printf("First value: %d\n", ptr_one->i); printf("Second value: %f\n", ptr_one->PI); printf("Third value: %c\n", ptr_one->A); “rec” is not necessary for given/left code, but is necessary for below code update For below, without RECORD, warning: useless storage class specifier in empty declaration
struct mystruct { int a; char b; } ; //note: could put st here instead struct mystruct st; char pb = (char)&st + offsetof(struct mystruct, b);*
struct SELF_REF { int a; struct SELF_REF b; int c; } ;
struct SELF_REF { int a; struct SELF_REF *b; int c; } ;
typedef struct { int a; struct SELF_REF *b; int c; } SELF_REF ;
typedef struct SELF_REF_TAG { int a; struct SELF_REF_TAG *b; int c; } SELF_REF ;
/* electronic cash register individual transaction receipt */ #define PRODUCT_SIZE 20; typedef struct { char product[PRODUCT_SIZE]; int qty; float unit_price; float total_amount; } Transaction; void print_receipt (Transaction trans) { printf(“%s\n, trans.product); printf(%d @ %.2f total %.2f\n”, trans.qty, trans.unit_price, trans.total_amount); } Function call: print_receipt(current_trans); Copy by value copies 32 bytes to the stack which can then be discarded later Instead… *(Transaction trans) trans->product // fyi: (trans).product trans->qty trans->unit_price trans->total_amount print_receipt(¤t_trans); void print_receipt(Transaction trans)
Dynamic Memory Allocation (again?!)
int *pi_save, *pi; pi = malloc(20); if (pi == NULL) { printf(“Out of memory!\n”); exit(1); } for (int x = 0; x < 5; x +=1) *pi++ = 0; // print