C By Discovery (4th Edition)

Info iconThis 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background 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 &lt;stdio.h&gt; void nochange(int x, int y); void change(int x, int *y); int main(void) { int x, y; x = 1; y = 2; change(x, &amp;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

This note was uploaded on 02/06/2011 for the course CSE 1320 taught by Professor Sprintston during the Spring '08 term at UT Arlington.

Page1 / 2

Lecture06 - legal memory address int_ptr = NULL; if...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online