Array and pointer in C | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Friday, July 3, 2020

Array and pointer in C | My CS Tutorial

Array and pointer in c | Array of  pointer | Pointer to array in c programing | Relationship between array and pointer | Difference between array and pointer | C programing array and pointer | Array transformation rule |  My CS Tutorial

Array and pointer in C | My CS Tutorial


Pointers and Arrays


In C, pointers and arrays are closely related. Pointers are very useful in array manipulation. Declaration of a pointer to an array is just like an integer pointer or a float pointer.

 int *ptr;
 int arr[5];
 ptr = &arr[0];

Here the pointer contains the address of the first element of the array. C
provides another alternate way for pointing to the first element of an array,

ptr = arr;

Note: It is important to remember that the name of an array not followed by a
subscript, is a pointer to the first element in the array.

Once a pointer has been set to point to an element of an array, it is possible to
use the increment ++ and decrement -- operator to point to subsequent or
previous elements of the array, respectively.
But incrementing or
decrementing the pointer to access a memory location beyond an array's
bound produces a runtime error, and your program may crash or may
overwrite other data or code sections of your program.
So it is the responsibility of the programmer to use the pointer to an array wisely.
One more factor to consider is the size of the data type that the pointer points
to. Suppose an integer pointer is pointing to an integer array; when you
increment the pointer, the pointer points to the next element of the array.

But in reality the pointer will contain an address which is typically four bytes
greater than the address of the first element of the array.


Array Transformation rule


If arr1 is an array, then the expression arr1 + 1 is the address of the second
element of the array, regardless of arr1's data type. We can now use the
indirection operator * in front of the variable to retrieve the value stored at
this location.
Thus
 *(arr1 + 1)
gives the value stored in the array's second element. Parentheses are required because the indirection operator * has a higher precedence over the addition operator.

So we can use this transformation rule to convert any array reference to its equivalent pointer expression.

arr1[0] is equivalent to *(arr1 + 0)
 arr1[1] is equivalent to *(arr1 + 1)
 arr1[2] is equivalent to *(arr1 + 2)

Without the parentheses the expression *arr1 + 1 evaluates to something
totally different, as this retrieves the value stored in the array's first element
and adds 1 to it.


Memory Allocation and pointers


Array definition results in a block of memory being reserved by the operating system at the begining of program execution, this does not happen if an array is represented by a pointer variable. The use of a pointer variable requires a programmer to do some type of memory assignment before the array
elements are utilized. This is called Dynamic memory allocation .
The malloc() standard C library function is used for this purpose.
Example:-

#include <stdio.h>
main()
{
 int *x;
 float *y;
 x =(int *) malloc(10 * sizeof(int));
 /* reserves memory for 10 integers */
 y = (double *) malloc(10 * sizeof(double));
/* reserves memory for 10 floating point numbers */
}

Note: The type cast preceding the malloc() function must be same as the data type of the pointer variable.

Difference between arrays and pointers


There are some key differences between arrays and pointers. A pointer's value can be changed to point to some other memory location.
But the pointer represented by an array name cannot be changed. It is treated as a constant.

 float TotAmt[10];

 TotAmt ++; // illegal statement
 TotAmt -= 1; // illegal statement

One more point to remember is that array names are initialized to point to the first element in the array whereas pointers are uninitialized when declared.
They have to be explicitly initialized before usage otherwise a run time error will occur.



Array and pointer in c | Array of  pointer | Pointer to array in c programing | Relationship between array and pointer | Difference between array and pointer | C programing array and pointer | Array transformation rule |  My CS Tutorial


_________________________________________


Please share this post and blog link with your friends.For more programs use this blog.

If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment