{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

C By Discovery (4th Edition)

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

View Full Document Right Arrow Icon
1 Intermediate Programming CSE 1320 Lecture 06 Chapter 4: 4.5-4.8 Pointer Arithmetic Combine a pointer and an integer floatptr + 1 ++floatptr floatptr— Combine two pointers int k = floatptr1 – floatptr2 ; floatptr1 + floatptr2 (not legitimate) Assignment, comparison, cast (to another pointer or int) floatptr-1 floatptr floatptr+1 floatptr+2 n – sizeof(float) n n + sizeof(float) n + 2*sizeof(float) Contents Address Dereferencing a Pointer Variable Determine the value pointed to by a pointer *int_ptr: contents of the current address in int_ptr #include <stdio.h> int main(void) { char c, *ptr_c = &c; c = ‘Q’; printf(“%c %c\n”, c, *ptr_c); *ptr_c = ‘r’; printf(“%c %c\n”, c, *ptr_c); return 0; } Pointer Initialization A newly declared pointer will not reference a legal memory address – int_ptr = &intvar; – int_ptr1 = int_ptr2; – int_ptr = (int *)1000; (casting) NULL is defined to be 0 – stdio.h or #define NULL 0 Indicates that a pointer does not point at a
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
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: legal memory address – int_ptr = NULL; – if (int_ptr != NULL) NULL Pointer int *int_ptr; int int_array[10]; int_array is the address of first cell in array *int_array int_array[0] *(int_array+1) int_array[1] *(int_array+i) int_array[i] int_ptr = int_array; int_array = int_ptr; (illegal) Array as a Pointer 2 Passing Parameters to Functions • Passed by reference – Storage location is known so operations are performed directly – Function will be able to change the parameter • Passed by value (in C) – Copy is made of the current value – Original parameter will not be changed Passing Parameters to Functions #include <stdio.h> void nochange(int x, int y); void change(int x, int *y); int main(void) { int x, y; x = 1; y = 2; change(x, &y); printf(“%d %d\n”, x, y); return 0; } void change(int x, int *y) { *y = x; x += 5; printf(“%d %d\n”, x, y); }...
View Full 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