Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

h Comment on a part of the tutorial. Be sure to identify the source of your selection--that is, give the chapter and the topic. Explain

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedhimage text in transcribedComment on a part of the tutorial. Be sure to identify the source of your selection--that is, give the chapter and the topic. Explain why you chose that part and give your reaction to it--for example, did you find it helpful, interesting, surprising, or incorrect, or did you have another reaction to it?

CHAPTER 2: Pointer types and Arrays Okay, let's move on. Let us consider why we need to identify the type of variable that a pointer points to, as in: int *ptr; One reason for doing this is so that later, once ptr "points to something, if we write: *ptr = 2; the compiler will know how many bytes to copy into that memory location pointed to by ptr. If ptr was declared as pointing to an integer, 4 bytes would be copied.- Similarly for floats and doubles the appropriate number will be copied. But, defining the type that the pointer points to permits a number of other interesting ways a compiler can interpret code. For example, consider a block in memory consisting if ten integers in a row. That is, 40 bytes of memory are set aside to hold 10 integers. Now, let's say we point our integer pointer ptr at the first of these integers. Furthermore lets say that integer is located at memory location 100 (decimal). What happens when we write: ptr + 1; Because the compiler "knows" this is a pointer (i.e. its value is an address) and that it points to an integer (its current address, 100, is the address of an integer), it adds 4 to ptr instead of 1, so the pointer "points to the next integer, at memory location 104. Similarly, were the ptr declared as a pointer to a short, it would add 2 to it instead of 1. The same goes for other data types such as floats, doubles, or even user defined data types such as structures. This is obviously not the same kind of "addition" that we normally think of. In C it is referred to as addition using "pointer arithmetic", a term which we will come back to later. Similarly, since ++ptr and ptr++ are both equivalent to ptr +1 (though the point in the program when ptr is incremented may be different), incrementing a pointer using the unary ++ operator, either pre-or post-, increments the address it stores by the amount sizeof(type) where "type" is the type of the object pointed to. i.e. 4 for an integer). Since a block of 10 integers located contiguously in memory is, by definition, an array of integers, this brings up an interesting relationship between arrays and pointers. Consider the following: int my_array[] = {1,23,17,4,-5, 100}; Here we have an array containing 6 integers. We refer to each of these integers by means of a subscript to my_array, i.e. using my_array[0] through my_array(5). But, we could alternatively access them via a pointer as follows: int *ptr; 1 1:41 PM A Type here to search D 2/23/2020 Here we have an array containing 6 integers. We refer to each of these integers by means of a subscript to my_array, 1.e. using my_array[0] through my_array(5). But, we could alternatively access them via a pointer as follows: int *ptr; ptr = &my_array[@]; /* point our pointer at the first integer in our array */ And then we could print out our array either using the array notation or by dereferencing our pointer. The following code illustrates this: ----------- Program 2.1 -------- /* Program 2.1 from PIRTUT10.HTM 6/13/97 */ #include int my_array[] = {1,23,17,4,-5,100); int *ptr; int main(void) int i; ptr = {my_array[@]; /* point our pointer to the first element of the array */ printf(" "); for (i = 0; i int my_array[] = {1,23,17,4,-5,100); int *ptr; int main(void) int i; ptr = {my_array[@]; /* point our pointer to the first element of the array */ printf(" "); for (i = 0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions