{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chapter 11-p - Example 11.1 Write a program to print the...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Example 11.1 Write a program to print the address of a variable along with its value. The program shown in Fig.11.4, declares and initializes four variables and then prints out these values with their respective storage locations. Notice that we have used %u format for printing address values. Memory addresses are unsigned integers. ACCESSING ADDRESSES OF VARIABLES Program main() { char a; int x; float p, q; a = 'A'; x = 125; p = 10.25, q = 18.76; printf("%c is stored at addr %u.\n", a, &a); printf("%d is stored at addr %u.\n", x, &x); printf("%f is stored at addr %u.\n", p, &p); printf("%f is stored at addr %u.\n", q, &q); } Output A is stored at addr 4436. 125 is stored at addr 4434. 10.250000 is stored at addr 4442. 18.760000 is stored at addr 4438. Fig.11.4 Accessing the address of a variable Example 11.2 Write a program to illustrate the use of indirection operator '*' to access the value pointed to by a printer. The program and output are shown in Fig.11.5. The program clearly shows how we can access the value of a variable using a pointer. You may notice that the value of the pointer ptr is 4104 and the value it points to is 10. Further, you may also note the following equivalences: x = *(&x) = *ptr = y &x = &*ptr ACCESSING VARIABLES USING POINTERS
Image of page 1

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

View Full Document Right Arrow Icon
Program main() { int x, y; int *ptr; x = 10; ptr = &x; y = *ptr; printf("Value of x is %d\n\n",x); printf("%d is stored at addr %u\n", x, &x); printf("%d is stored at addr %u\n", *&x, &x); printf("%d is stored at addr %u\n", *ptr, ptr); printf("%d is stored at addr %u\n", y, &*ptr); printf("%d is stored at addr %u\n", ptr, &ptr); printf("%d is stored at addr %u\n", y, &y); *ptr = 25; printf("\nNow x = %d\n",x); } Output Value of x is 10 10 is stored at addr 4104 10 is stored at addr 4104 10 is stored at addr 4104 10 is stored at addr 4104 4104 is stored at addr 4106 10 is stored at addr 4108 Now x = 25 Fig.11.5 Accessing a variable through its pointer Example 11.3 Write a program to illustrate the use of pointers in arithmetic operations.
Image of page 2
Image of page 3
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