Pointers & Arrays
Pointers और Arrays
Pointer and Array Relationship
In C, arrays and pointers are closely related. The array name usually represents the address of the first element. Because of this, array elements can be accessed using both index notation and pointer notation.
arr[i] is internally similar to *(arr + i). Both access the same element.
Array Name as Pointer
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
printf("Address of first element = %p\n", (void*)&arr[0]);
printf("Array name address = %p\n", (void*)arr);
printf("First element = %d\n", *arr);
printf("Third element = %d\n", *(arr + 2));
return 0;
}arr behaves like a pointer in expressions, but it is not a modifiable pointer variable. You cannot write arr++, but you can write ptr++.
Pointer Arithmetic with Arrays
When an integer pointer is increased by 1, it moves to the next integer element, not the next byte. The compiler automatically uses the size of the data type.
#include <stdio.h>
int main() {
int marks[] = {85, 90, 78, 92};
int *p = marks;
for(int i = 0; i < 4; i++) {
printf("marks[%d] = %d\n", i, *(p + i));
}
return 0;
}Passing Arrays to Functions
When an array is passed to a function, actually the address of its first element is passed. Therefore, changes made inside the function can affect the original array.
#include <stdio.h>
void update(int *a, int n) {
for(int i = 0; i < n; i++) {
a[i] = a[i] + 5;
}
}
void display(int a[], int n) {
for(int i = 0; i < n; i++) {
printf("%d ", *(a + i));
}
}
int main() {
int arr[] = {10, 20, 30};
update(arr, 3);
display(arr, 3);
return 0;
}Summary
arrnormally gives the base address of the arrayarr[i]and*(arr+i)access the same value- Pointer arithmetic moves according to data type size
- Arrays passed to functions can be handled using pointers
- Array name is not a modifiable pointer, so
arr++is invalid
Write programs to find sum, maximum, minimum and reverse of an array using pointers.
Pointer और Array Relationship
C में arrays और pointers का बहुत close relation होता है। Array का नाम अक्सर first element का address represent करता है। इसलिए array elements को index और pointer दोनों methods से access कर सकते हैं।
arr[i] लगभग *(arr + i) जैसा ही काम करता है। दोनों same element access करते हैं।
Array Name as Pointer
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
printf("First element address = %p\n", (void*)&arr[0]);
printf("Array name address = %p\n", (void*)arr);
printf("First value = %d\n", *arr);
printf("Third value = %d\n", *(arr + 2));
return 0;
}arr expression में pointer की तरह behave करता है, पर यह modifiable pointer variable नहीं है। इसलिए arr++ invalid है।
Pointer Arithmetic
Pointer में 1 add करने पर वह next element पर जाता है। जैसे int * pointer next integer पर जाएगा।
#include <stdio.h>
int main() {
int marks[] = {85, 90, 78, 92};
int *p = marks;
for(int i = 0; i < 4; i++) {
printf("%d ", *(p + i));
}
return 0;
}Array को Function में भेजना
Function में array भेजने पर उसका base address जाता है। इसलिए function के अंदर changes original array को affect कर सकते हैं।
void change(int *a, int n) {
for(int i = 0; i < n; i++)
a[i] += 10;
}सारांश
- Array name base address देता है
arr[i]और*(arr+i)same value access करते हैं- Pointer arithmetic data type size के अनुसार move करती है
- Array function में pointer की तरह pass होता है
- Practice के लिए sum, max, reverse pointer से करें