In C programming pointer is used for dynamically allocation of memory blocks. For each array element increment index and arr by 1 till arr < arrEnd and arr toSearch. C program to access and modify array elements using the Pointer data type. Initialize a variable index 0 that will keep track of searched element index. Say arr array and arrEnd (array + size - 1). This can be seen in the following code snippet. Initialize a pointer to first and last element of array. The pointer is incremented in each iteration of the loop i.e at each loop iteration, the pointer points to the next element of the array. int *ptr = &arr Īfter this, a for loop is used to dereference the pointer and print all the elements in the array. In the above program, the pointer ptr stores the address of the first element of the array. } Output The values in the array are: 1 2 3 4 5 In other words, pointers reference a memory location and obtaining the value stored at that memory location is known as dereferencing the pointer.Ī program that uses pointers to access a single element of an array is given as follows − Example Pointers store the memory location or address of variables.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |