C Programming Pointers and Files PDF
Document Details
Uploaded by BalancedBinomial2004
Dr.Sreeparna Chakrabarti
Tags
Summary
This document provides an introduction to pointers and files in C programming. It explains the concepts of pointers, pointer initialization/address operators, pointer arithmetic, and file handling techniques. The content includes examples and syntax to illustrate file operations.
Full Transcript
Unit 5: Pointers and Files Dr.Sreeparna Chakrabarti Faculty Member Dept.of CS KJC Pointers Pointer is a variable that points to or references a memory location in which the data is stored. It contains the memory location of another variable. Unlike other variables that hold values of a...
Unit 5: Pointers and Files Dr.Sreeparna Chakrabarti Faculty Member Dept.of CS KJC Pointers Pointer is a variable that points to or references a memory location in which the data is stored. It contains the memory location of another variable. Unlike other variables that hold values of a certain type, pointer holds the address of a variable. For example, an integer variable holds (or you can say stores) an integer value, however an integer pointer holds the address of a integer variable. Pointers #include void main() { int num; int *ptr; num=10; ptr=# // Declare an integer variable printf("Value of variable num is: %d\n", num); // Print the value of num printf("Address of variable num is: %u\n", &num); // Print the address of num printf("Value stored in pointer ptr is: %u\n", ptr); // Print the address stored in ptr printf("Value pointed to by ptr is: %d\n", *ptr); // Dereference ptr to get the value of num } Pointers General Syntax to declare a pointer : datatype * variable name; where datatype denotes the variable data type being pointed to and asterisk denotes a pointer. Pointer Operator: Pointer operator can be represented by the combination of * (asterisk) with variable. i.e. int *ptr; Pointers Base type of the pointer defines which type of variable the pointer is pointing to Pointer Arithmetic is done relative to the base datatype. The number of bytes required to store particular data item will vary from machine to machine however, the length of various data types are, Character - 1 byte ,Integer - 2 bytes,Float - 4 bytes,Long integer - 4 bytes,Double - 8 bytes but the size of the pointer irrespective of the datatype it is pointing to, remains the same depending on the machine. Pointers Pointer Initialization/Address Operator Pointer has to be assigned the address of the variable that it is pointing to which is done as follows: i.e. int *ptr, int i=1; ptr = & i; Pointers Pointers #include void main ( ) { int *ptr, num; num = 45; ptr = # printf (“%d is stored at %u \n”, *ptr, ptr); } Note:The asterisk tells the compiler that you are interested in the contents of the memory location stored in the pointer "*ptr" and only the pointer name "ptr" says you are interested in address. Pointer Arithmetic: In C, pointer arithmetic allows you to perform arithmetic operations on pointers, but there are specific rules. Let's break down the valid and invalid operations with examples: (a)*ptr1 = *ptr1 + *ptr2; This means you are dereferencing both pointers and adding the values stored at the memory locations they point to. Afterward, the sum is assigned to the location pointed to by ptr1. Pointer Arithmetic: #include void main() { int a = 5, b = 10; int *ptr1 = &a, *ptr2 = &b; *ptr1 = *ptr1 + *ptr2; // Add values pointed by ptr1 and ptr2, store result in //*ptr1 printf("Value of a: %d\n", a); // Output: Value of a: 15 } Pointer Arithmetic: In C, pointer arithmetic allows you to perform arithmetic operations on pointers, but there are specific rules. Let's break down the valid and invalid operations with examples: (b) sum = *ptr1 + 10; This adds 10 to the value stored at the memory location pointed to by ptr1. Pointer Arithmetic: #include void main() { int a = 5; int *ptr1 = &a; int sum; sum = *ptr1 + 10; // Add 10 to the value pointed by ptr1 (5 + 10 = 15) printf("Sum: %d\n", sum); // Output: Sum: 15 } Pointer Arithmetic: In C, pointer arithmetic allows you to perform arithmetic operations on pointers, but there are specific rules. Let's break down the valid and invalid operations with examples: (c) sum += *ptr2; This adds the value stored at the memory location pointed to by ptr2 to sum. Pointer Arithmetic: #include void main() { int sum = 20, b = 5; int *ptr2 = &b; sum += *ptr2; // Add value pointed by ptr2 to sum (20 + 5 = 25) printf("Sum: %d\n", sum); // Output: Sum: 25 } Pointer Arithmetic: In C, pointer arithmetic allows you to perform arithmetic operations on pointers, but there are specific rules. Let's break down the valid and invalid operations with examples: (d) ptr1 > ptr2; This checks if the memory address stored in ptr1 is greater than that stored in ptr2. This comparison only makes sense if ptr1 and ptr2 point to elements of the same array. Pointer Arithmetic: #include void main() { int arr = {1, 2, 3}; int *ptr1 = &arr; // Points to the last element int *ptr2 = &arr; // Points to the first element if (ptr1 > ptr2) { printf("ptr1 points to a higher address than ptr2\n"); // This will print } } Pointer Arithmetic: In C, pointer arithmetic allows you to perform arithmetic operations on pointers, but there are specific rules. Let's break down the valid and invalid operations with examples: (e) ptr1 != ptr2; This checks if the memory addresses stored in ptr1 and ptr2 are different. Pointer Arithmetic: #include void main() { int a = 5, b = 10; int *ptr1 = &a, *ptr2 = &b; if (ptr1 != ptr2) { printf("ptr1 and ptr2 point to different addresses\n"); // This will print } } Pointer Arithmetic: Invalid Pointer Operations a) ptr1 + ptr2; You cannot add two pointers because it doesn't make sense to add two memory addresses. This operation is not allowed and will cause a compilation error. b) ptr1 * ptr2; Multiplying two pointers is also invalid because memory addresses cannot be multiplied in a meaningful way. c) ptr1 / ptr2; Dividing two pointers is not allowed for the same reason. It doesn't make sense to divide two memory addresses. Pointer Arithmetic: Invalid Pointer Operations Note: Subtracting two pointers is allowed but only if they point to elements of the same array. The result of the subtraction is the difference in the number of elements between the two pointers, which is an integer value. This operation gives you the number of elements between the two pointers, not the byte difference. This is a complicated operation and will be avoided for the time being Accessing a variable through its pointer: #include void main() { //normal variable int num = 100; //pointer variable int *ptr; //pointer initialization ptr = # //pritning the value printf("value of num = %d\n", *ptr); } Pointers and Functions Usage of a pointer in a function definition may be classified into 2 groups. Call by Value Call by Reference Call by Value: When a function is invoked correspondence is established between the formal and actual parameters. A temporary storage is created where the value of the actual parameter is stored. The formal parameter picks up its value from this storage area. This mechanism of data transfer is called call-by-value. Pointers and Functions Call by value example: #include void change(int num) { printf("Before adding value inside function num=%d \n",num); num=num+100; printf("After adding value inside function num=%d \n", num); } void main() { Output: int x=100; Before function call x=100 printf("Before function call x=%d \n", x); Before adding value inside function num=100 After adding value inside function num=200 change(x);//passing value in function After function call x=100 printf("After function call x=%d \n", x); } Pointers and Functions Call by reference Here we pass the address to a function, and the parameters receiving the address should be pointers. This process is called call by reference. The function which is called by reference can change the value of the variable used in the call. Pointers and Functions Call by reference example #include void change(int *num) { printf("Before adding value inside function num=%d \n", *num); *num = *num + 100; printf("After adding value inside function num=%d \n", *num); } void main() { int x = 100; Output: printf("Before function call x=%d \n", x); Before function call x=100 change(&x); // Passes the address of x to the function Before adding value inside function num=100 printf("After function call x=%d \n", x); After adding value inside function num=200 } After function call x=200 Pointers and Arrays Array name is like a pointer but not the same i.e. for the array a, a contains the address of the first element a. Eg: int a, *ptr; Then the pointer could be assigned the address as ptr = &a; This is as assigning the array name to the pointer i.e ptr = a; The address of the 0th element is in ptr ptr++ is the address of a ptr+6 is the address of a *(ptr+1) is the content of a Example-Arrays & Pointers #include void main ( ) { int a, i, j, n, *ptr; printf("Enter the number of elements in the array\n"); scanf("%d",&n); printf("Enter array elements \n"); for (i=0; i