Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
Goeduhub's Online Courses @ Udemy in Just INR 570/-
Online Training - Youtube Live Class Link
0 like 0 dislike
153 views
in Tutorial & Interview questions by Goeduhub's Expert (8.3k points)

Iterating through an array using pointers

Goeduhub's Top Online Courses @Udemy

For Indian Students- INR 360/- || For International Students- $9.99/-

S.No.

Course Name

 Coupon

1.

Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon

2.

Natural Language Processing-NLP with Deep Learning in Python Apply Coupon

3.

Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon
    More Courses

1 Answer

0 like 0 dislike
by Goeduhub's Expert (8.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.

3.3k questions

7.1k answers

394 comments

4.6k users

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
...