Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
+1 vote
711 views
in Computer by (69.8k points)

Differentiate between pointers and arrays? Write a C program to display the contents of an array using a pointer arithmetic. 

1 Answer

+1 vote
by (69.2k points)
selected by
 
Best answer

Pointer and arrays: 

Pointers and arrays are very closely linked in C. Consider the following statements: 

int a[10], x;

int *ptr; /* ptr is a pointer variable*/

ptr = &a[0]; /* ptr points to address of a[0] */

x = *ptr;

/* x = contents of ptr (a[0] in this case) */

A pointer is a variable so we can do 

ptr = a and ptr++ ; 

while an array is not a variable so statements

a = pa and a++ are illegal.

A C program to display the contents of an array using a pointer arithmetic is listed below:

//display the contents of an array using pointer

#include < stdio. h >

void main() 

int *p,sum,i;

static int x[5] = {5,9,6,3,7};

i=0;

p=x;

sum=0;

clrscr();

printf("\nElement Value Address\n\n");

while(i<5)

{

printf(" x[%d] %d %u\n",i,*p,p); 

sum+=*p;

i++;

*p++;

}

printf("\n Sum = %d\n",sum); 

printf("\n &x[0] = %u\n",&x[0]); 

printf("\n p = %u\n",p);  

getch();

}

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...