Practice10 - Reminder - Pointers and Arrays Recall that an...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Reminder - Pointers and Arrays Recall that an array S holds the address of its first element S[0] S is actually a pointer to S[0] int S[10]; int *P; P=S; /* From now P is equivalent to S */ Both P and S are now pointing to S[0] Pointer-array equivalence Arrays are actually a kind of pointers! When an array is defined, a fixed amount of memory the size of the array is allocated. The array variable is set to point to the beginning of that memory segment When a pointer is declared, it is uninitialized (like a regular variable) Unlike pointers, the value of an array variable cannot be changed Pointer arithmetic Pointers can be incremented and decremented If p is a pointer to a particular type, p+1 yields the correct address of the next variable of the same type p++ , p+i , and p += i also make sense Pointer arithmetic - example If p and q point to elements in an array, q-p yields the number of elements between p and q. Example for the use of pointer arithmetic – strcpy.c array_strcpy void array_strcpy( char dest, char src) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; } array_strcpy – step by step void array_strcpy( char dest, char src) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; } ‘y’ ‘e’ ‘s’ ‘\0’ ‘%’ src dest i ‘!’ ‘!’ ‘!’ ‘!’ ‘!’ array_strcpy – step by step void array_strcpy( char dest, char src) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; } ‘y’ ‘e’ ‘s’ ‘\0’ ‘%’ src dest i ‘!’ ‘!’ ‘!’ ‘!’ ‘!’ array_strcpy – step by step void array_strcpy( char dest, char src) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’ ‘e’ ‘s’ ‘\0’ ‘%’ src dest 1 i ‘y’ ‘!’ ‘!’ ‘!’ ‘!’ array_strcpy – step by step void array_strcpy( char dest, char src) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’ ‘e’ ‘s’ ‘\0’ ‘%’ src dest 1 i ‘y’ ‘!’ ‘!’ ‘!’ ‘!’ array_strcpy – step by step void array_strcpy( char dest, char src) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’ ‘e’ ‘s’ ‘\0’ ‘%’ src dest 1 i ‘y’ ‘e’ ‘!’ ‘!’ ‘!’ array_strcpy – step by step void array_strcpy( char dest, char src) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’ ‘e’ ‘s’ ‘\0’ ‘%’ src dest 2 i ‘y’ ‘e’ ‘!’ ‘!’ ‘!’ array_strcpy – step by step...
View Full Document

This note was uploaded on 01/10/2010 for the course CS 463 taught by Professor Can'tsay during the Spring '09 term at Haaga - Helia University of Applied Sciences.

Page1 / 141

Practice10 - Reminder - Pointers and Arrays Recall that an...

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

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