130 likes | 227 Views
CSCI 171. Presentation 14 Structures and pointers. Pointers to structures. typedef struct { double width; double length; } rectangle; rectangle * r_ptr = NULL, r; r.width = 3.2; r.length = 7.3; r_ptr = &r;. Pointers to Structures. From our previous slide: r.width = 3.78;
E N D
CSCI 171 Presentation 14 Structures and pointers
Pointers to structures • typedef struct { • double width; • double length; • } rectangle; • rectangle * r_ptr = NULL, r; • r.width = 3.2; • r.length = 7.3; • r_ptr = &r;
Pointers to Structures • From our previous slide: • r.width = 3.78; • (*r_ptr).width = 3.78; • r.length = 4.2; • (*r_ptr).length = 4.2; • }Equivalent • }Equivalent
Another way to access members of a ‘pointed to’ structure • Indirect Membership Operator (->) • dash followed by the greater than sign • Place this between the member and the pointer name to access data: • r_ptr -> width;
Indirect Membership Operator • typedef struct { • double width; • double length; • } rectangle; • rectangle * r_ptr = NULL, r; • r_ptr = &r; • r.length = 4.2; • (*r_ptr).length = 4.2; • r_ptr -> length = 4.2; • }Equivalent
Declaring a Pointer Member • typedef struct { • double * width; • double * length; • } rectangle; • rectangle r; • if ((r.width = (double *)malloc(sizeof(double))) == NULL) { • printf(“Error allocating memory at 1”); • exit(1); • } • if ((r.length = (double *)malloc(sizeof(double))) == NULL) { • printf(“Error allocating memory at 1”); • exit(1); • } • *(r.width) = 3.2; • *(r.length) = 7.5;
Ex: Ptrs to structs with ptr members typedef struct { double * width; double * length; } rectangle; … rectangle * r_ptr = NULL; if ((r_ptr = (rectangle *)malloc(sizeof(rectangle)))==NULL) … if ((r_ptr -> width = (double *)malloc(sizeof(double))) == NULL) … if ((r_ptr -> length = (double *)malloc(sizeof(double))) == NULL) … printf("Enter width: "); scanf("%lf", r_ptr -> width); printf("Enter length: "); scanf("%lf", r_ptr -> length); printf("The area is: %lf", *(r_ptr -> width) * *(r_ptr -> length));
Pointers to Arrays of Structures • typedef struct { • double width; • double length; • } rectangle; • rectangle r[100], *ptr = NULL; • ptr = r;
Accessing elements via a pointer • Other elements in the array can be accessed with pointer arithmetic • Incrementing a pointer that points to an array of instances of structures will cause the pointer to point to the next instance • Size is determined automatically
Accessing elements via a pointer • We can use iteration to loop thru the elements
Accessing elements via a pointer • Sample code to print out the width member of each element (i.e. each structure instance) for (i = 0; i < 100; i++) printf(“\nThe width of element %d is %lf”, (i + 1), (ptr+i)->width);
Dynamic allocation if number of elements is unknown rectangle * r_ptr = NULL; int num = 0; printf(“How many rectangles: “); scanf(“%d”, &num); if ((r_ptr = (rectangle *)malloc(num * sizeof(rectangle)))==NULL) … for (i = 0; i < num; i++) { printf(“\nEnter width of element %d: ”, (i + 1)); scanf(“%lf”, &((ptr + i) -> width)); printf(“\nEnter length of element %d: ”, (i + 1)); scanf(“%lf”, &((ptr + i) -> length)) }
Dynamic Data Structures • Linked Lists • Queues • FIFO • Stacks • LIFO • Trees