Ví dụ về sử dụng con trỏ với mảng

#include <stdio.h>

int findFirstOccurrence(int *arr, int size, int x) {
int *ptr = arr;
for (int i = 0; i < size; i++) {
if (*ptr == x) {
return i;
}
ptr++;
}
return -1;
}

int main() {
int arr[] = {2, 3, 4, 5, 7, 9, 11, 13};//{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 4, 5, 8};//
int size = sizeof(arr) / sizeof(arr[0]);
int x = 6; //x is the one we want to find
int result = findFirstOccurrence(arr, size, x);
if (result == -1) {
printf("Number not found in the array.\n");
} else {
printf("Number found at index %d.\n", result);
}
return 0;
}