If you try to access elements beyond the last element

Info icon This preview shows pages 6–14. Sign up to view the full content.

View Full Document Right Arrow Icon
` If you try to access elements beyond the last element, this will produce a run-time error (typically a segmentation fault) OR will read or write a value that is not an element of the array (a harder bug to find).
Image of page 6

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
` There is no library function for copying arrays in C (except for strings, which we’ll see soon). ` If you want to copy an array in C, you must copy the elements one by one (with a for or while loop). For example, we can use something such as: int scores[6] = {19, 17, 18, 16, 15, 20}; /* Array to be copied */ int copy[6]; /* Copy of original array */ for (i = 0; i < 6; i++) { copy[i] = scores[i]; }
Image of page 7
` int scores[6] = {19, 17, 18, 16, 15, 20}; ` In C, the name of the array by itself is a constant pointer to the first element of the array; that is, scores is the same as &scores[0] (don’t use &scores, though!) ` Because this pointer is a constant, it cannot be changed (for example, you cannot assign a different address to it). ` All of the elements of the array are stored in contiguous memory locations.
Image of page 8

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
` The compiler generates instructions to compute the exact address for any chosen element of the array when using an array; it can do this because it knows all the bytes used to store the array are contiguous. ` Therefore, to access scores[3], for example, the compiler generates instructions to compute the address as: scores + ( 3 * ( sizeof ( int ) ) ) ` Recall that scores is a constant pointer to the 1 st element, so the compiler can compute the address above as: &scores[0] + ( 3 * ( sizeof ( int ) ) )
Image of page 9
` Declaration example: void *ptr; ` A pointer declared to point to a certain type should not be assigned the address of an object of a different type (without casting): float *float_ptr; int int_var = 5; float_ptr = &int_var; /* Dangerous - the compiler will give a warning here, because there is no explicit cast! */
Image of page 10

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
` The exception is pointer to void (void *), which is assignment compatible with pointers to other data types: /*With declarations above – valid */ void_ptr = float_ptr; /*With declarations above – also valid*/ int_ptr = void_ptr;
Image of page 11
` A pointer to void cannot be dereferenced without casting (because the compiler won’t know how to generate an appropriate instruction to interpret the data which is pointed to by the pointer): ` Another way to say this is that - without casting - the compiler won’t know how many bytes to read starting at the given address void *void_ptr; int var1 = 1; void_ptr = &var1; printf(“*void_ptr equals %d”, *void_ptr); /* Invalid – dereference of void pointer without cast */ printf(“*void_ptr equals %d”, * (int *) void_ptr) ); /* Valid */ /* NOTE: the dereference operator has higher precedence than the cast, or type conversion, operator */
Image of page 12

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
` Why use pointers to void? ` One purpose for them is that certain C library functions which allocate memory dynamically (i.e., at run time) return a void *, or pointer to void (because otherwise, there would have to be a different version of the library function to return each pointer type), which can then be assigned to a pointer to any other type.
Image of page 13
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern