I need someone's help to explain arrays to me simply and pointers plz | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I need someone's help to explain arrays to me simply and pointers plz

7th Jul 2016, 12:29 PM
TheFallenNights
TheFallenNights - avatar
4 Answers
+ 3
an array is simply memory allocated and holds/will hold up to a specified amount of data. so of we have int myArray [5]; this sets aside 20 bytes of memory so we can store 5 integer values there. let's say we have 1 2 3 4 5 stored there memory addresses should be 4 bytes apart. and this s where pointers come into play. let's say we wanted to know what those address where, so we can declare an integer type pointer variable that can look up integer data. this pointer variable is allowed access to memory locations. so of we use these two together in a small loop: int myArray []={1,2,3,4,5};//array set to size 5 int* ptr; for (int I =0;i <5;i++) {ptr=&myArray [i]; cout <<ptr <<endl; } this will loop through each of the arrays indexes (the number inside the []). also, array indexing starts at 0, so that's why the loop is like that
7th Jul 2016, 4:26 PM
destro
+ 1
http://www.sololearn.com/app/cplusplus/playground/cbZ46undNaW4/ here's an example program so you can see the outputs
7th Jul 2016, 4:28 PM
destro
0
@destroy thx for the help
7th Jul 2016, 6:08 PM
TheFallenNights
TheFallenNights - avatar
0
pointers are those functions that indicate (point) where the data is stored. basically it gives the address of the particular data.
8th Jul 2016, 4:24 PM
Abbas Tailor