Pointer and Arrays

Please refer to these sites before learning the tutorial below to understand the topic in more depth.


C Pointers

In this tutorial, you will learn about the use of an array using pointers. Whenever we declare an array, the compiler will allocate the base address. The compiler will also allocate sufficient memory for storing all the elements in the contiguous memory locations. The base address of the array means the location of the first element of the array. The compiler will also define the array name as a constant pointer to the first element.

Let us suppose that we have an array array which is an array of character datatype. The first memory location of the array is 100. Hence, the consecutive memory locations will be 101, 102, 103 respectively as a single character will allocate 1 byte of memory.

Now let’s move on to a new example of implementing both pointer and array for computing the sum of all elements

int main(){
	int n[10], *p, i, sum=0, n1;
	printf("How many elements do you want to enter? ");
	scanf("%d", &n1);
			printf("Enter all numbers: ");
	for(i=0; i<n1; i++){
		scanf("%d", (p+i));
		sum = sum + *(p+i);
	printf("Sum = %d", sum);

The above code will display its output as below:

How many elements do you want to enter? 5

Enter all numbers: 1





Sum  = 15


SHARE Pointer and Arrays

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *