1 / 13

CSCI 171

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;

acton-buck
Download Presentation

CSCI 171

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI 171 Presentation 14 Structures and pointers

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

  3. 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

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

  5. 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

  6. 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;

  7. 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));

  8. Pointers to Arrays of Structures • typedef struct { • double width; • double length; • } rectangle; • rectangle r[100], *ptr = NULL; • ptr = r;

  9. 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

  10. Accessing elements via a pointer • We can use iteration to loop thru the elements

  11. 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);

  12. 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)) }

  13. Dynamic Data Structures • Linked Lists • Queues • FIFO • Stacks • LIFO • Trees

More Related