C++ Pointers Lecture Notes (3 Dec 2024) PDF
Document Details
Uploaded by MercifulMoldavite109
2024
Tags
Summary
These lecture notes are on pointers in C++. The document clearly demonstrates how pointers work with arithmetic operations, arrays, and more. It provides explanations and examples to aid comprehension.
Full Transcript
Structured Programming using C++ Lecture 9 (3 Dec 2024) Pointers Arithmetic 2 Pointers Arithmetic Ex. For ordinary arithmetic 3000 3004 operations:...
Structured Programming using C++ Lecture 9 (3 Dec 2024) Pointers Arithmetic 2 Pointers Arithmetic Ex. For ordinary arithmetic 3000 3004 operations: 5 3000 – 3000 + 2 = 3002 x xPtr For pointers arithmetic: 3000 3004 3012 – 3000 + 2 is not always 3002 5 3000 3008 Ex: x xPtr ptr int x = 5, *xPtr = &x; int *ptr = xPtr + 2; Output ptr value = 3008 printf(“ptr value = %p.”, ptr); 3 Pointers Arithmetic cont. type1 *namePtr = &var; Declare a pointer called namePtr that points to variables of type type1 and assign the address of variable var to it. type1 *name2Ptr = namePtr + C; Declare a pointer called name2Ptr that points to variables of type type1 and assign the value of the pointer namePtr to it after adding C value to the namePtr value. Now, name2Ptr_value = namePtr_Value + (C * sizeof(type1)) Ex: long *nPtr = &x; //assume x is located in memory location 1000 long *ptr = nPtr + 3; printf(“ptr value = %p.”, ptr); //ptr = 1000 + (3 * sizeof(long)) = 1024 4 Pointers Arithmetic cont. int b; int *bPtr = b; 2000 2004 2008 2012 2016 bPtr += 3; 2000 2008 2012 b b b b b bPtr bptr--; int x = bPtr – b; X=2 X = (2008 – 2000)/sizeof(int) X = the difference in offsets 5 Pointers Arithmetic cont. Take care: Do not use pointer arithmetic unless you deal with arrays. – Because, arrays guarantee that there are several objects of the same type stored contiguously in memory. A pointer can be assigned to a pointer of the same type. – Otherwise, you need casting before assignment. 6 Pointers Arithmetic cont. int x = 4, y = 9, *xPtr = &x, *yPtr = &y; float f = 3.2, *fPtr = &f; xPtr = yPtr; //GOOD xPtr = fPtr; //ERROR xPtr = (int *)fPtr; //GOOD 7 Arrays vs Pointers Arrays: A group of items of the same type that can be referenced by a name. Each single item of this group can be referenced by the group name and an index of this item. Pointers: Variables that can store addresses of other variables of a given type. Both are very close. You can think of array as a constant pointer. Pointers can be used in any array operation. Compilers convert array subscription notation into pointer notation. Arrays guarantee that there is a contiguous space of memory of the same type allocated. 8 Arrays vs Pointers cont. int a, *aptr = NULL; aptr = a; // → aptr = &a; a++; // → *(a+3)++; *(aptr+3)++ *(aptr+3) *aptr+3 x+ a is equivalent to &a + (3 * sizeof(int)) Also, a is equivalent to a + (3 * sizeof(int)) a → *(a + 3) → *(aptr + 3) → aptr aptr can point to different data → aptr = &a; Array a can not point to any location but the array starting point. (const pointer) → a = &a; is wrong. 9 Arrays vs Pointers 01.cpp void cpyArr(char s1[], const char s2[]){ int main() { int i; char str1, *str2 = "Hello"; for(i=0; s2[i]!='\0'; ++i) char str3, str4[] = "Good Bye"; s1[i] = s2[i]; cpyArr(str1, str2); s1[i] = '\0'; printf("String 1 = %s.\n", str1); } cpyPtr(str3, str4); printf("String 3 = %s.\n", str3); void cpyPtr(char *s1, const char *s2){ return 0; for( ; *s2 != '\0'; ++s1, ++s2) } *s1 = *s2; *s1 = '\0'; } 10 Pointers Riddle int a = {11, 23, 2, 18, 50, 6, 7, 83, 9, 0}; int *x = a; cout