Summer training at Goeduhub Technologies-Jaipur

Online Instructor-Led Live Training Courses/Workshops details || Claim for online workshop's certificate

0 like 0 dislike
30 views
in Tutorial & Interview questions by Goeduhub's Expert (7.3k points)

Iterating through an array using pointers

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.3k points)
 
Best answer
#include <stdio.h>

#define SIZE (10)

int main()

{    

size_t i = 0;    

int *p = NULL;    

int a[SIZE];       /* Setting up the values to be i*i */    

for(i = 0; i < SIZE; ++i)    

{        

a[i] = i * i;    

}       /* Reading the values using pointers */    

for(p = a; p < a + SIZE; ++p)    

{        

printf("%d\n", *p);    

}

return 0;

}

Here, in the initialization of p in the first for loop condition, the array a decays to a pointer to its first element, as it would in almost all places where such an array variable is used.

Then, the ++p performs pointer arithmetic on the pointer p and walks one by one through the elements of the

array, and refers to them by dereferencing them with *p.

Related questions

0 like 0 dislike
1 answer
 Know About Popular Colleges/Universities  List of IITsList of NITs | RTU-Kota | BTU-Bikaner | Manipal University-Jaipur | JECRC University | Amity University Jaipur | BIT Mesra-Jaipur | MODY UNIVERSITY | LNMIIT-Jaipur | JK Lakshmipat | Banasthali Vidyapith | POORNIMA University
 Exams:   List of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG) | JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs
Placements:  List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
 Online Free Training:  Artificial Intelligence(AI) & Machine Learning(ML)Python Programming | Internet of Things-IoT | OpenCV (Open Source Computer Vision Library) | LINUX | Big Data : Hadoop | 
 Goeduhub
About Us | Contact Us   Social::   |  | 
...