Contiguous bytes and the elements contained in it can

Info icon This preview shows pages 28–34. Sign up to view the full content.

View Full Document Right Arrow Icon
contiguous bytes and the elements contained in it can be accessed exactly like those in a statically allocated array. ` Thus, we can also use subscripts or indexes, as we do for statically allocated arrays. For example, we can access the third integer in the space allocated by malloc(), and pointed to by p, with p[2]. ` We can access elements in statically allocated arrays in C using either of these methods as well, i.e., either with subscripts or with pointer arithmetic. ` Access via the second method may be easier for those coming from Java and C++, however, both methods will be required to do well on the midterm.
Image of page 28

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

View Full Document Right Arrow Icon
` Be careful when you use the dereference operator with a pointer to elements of a statically or dynamically allocated array, along with pointer arithmetic (suppose p points to the 1 st of 5 integers): *(p + 3) = 45; /* Assigns 45 to 4 th int */ *p + 3 = 45; /* Invalid - Why? */
Image of page 29
` What appears on the left side of an assignment operator in C has to be an L- value , that is, a location in memory where a value can be stored. ` What appears on the right side of an assignment operator in C has to be an R-value , that is, a numeric value which can be stored in a binary form. ` In the invalid expression on the previous slide, we have: *p + 3 = 45; *p + 3 is not an L-value, however, because it is not a location in memory!
Image of page 30

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

View Full Document Right Arrow Icon
` When you use pointers to access dynamically allocated storage, be sure that you do not use a pointer value that will attempt to access space outside the allocated storage! ` This will result in a segmentation fault typically. ` As we stated before, C has no library function which returns the size of an array, so you have to keep track of it explicitly, and pass it as a parameter to any function which accesses elements of an array (statically or dynamically allocated).
Image of page 31
` When functions are called in C, the parameters to the function are passed by value : int a = 5; int b = 10; func1(a, b); ` What this means is that the values of a and b in the calling environment will be passed to func1, but func1 will not have access to the memory where a and b are stored in the calling function, so it cannot alter their values. ` The values of the parameters are placed on the stack (the values are copied from the variables in the calling function, and written to the stack), before the function begins execution.
Image of page 32

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

View Full Document Right Arrow Icon
` Normally, it is desirable that the called function not be able to change the values of variables in the calling environment, because this limits the interaction between the calling function and the called function, and makes debugging and maintenance easier. ` At times, though, we may want to give a function access to the memory where the parameters are located. ` In some cases in C, this is the only way we can pass a parameter; for example, elements of an array cannot be passed by value (unless each of the elements is passed as an individual parameter).
Image of page 33
Image of page 34
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