



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
Examples of defining and manipulating structures with typedef and pointer variables in C. It also covers the concept of offsetof and its usage for calculating the memory offset of a structure member.
Typology: Schemes and Mind Maps
1 / 5
This page cannot be seen from the preview
Don't miss anything!




struct mystruct { int a; char* b; } ; //note: could put st here instead struct mystruct st; char* pb = (char*)&st + offsetof(struct mystruct, b);
#include<stdio.h> typedef struct { 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);