A function(arrays and pointers)

0 Alex Carter · April 7, 2015
Hey, guys, I know it's a part of a code, it's only a function, but there's a part i really don't understand and, you know that feeling, it really disturbs, so, if anyone could explain it in simple language, I would be very grateful :) So there it is:

void putNewElementAtTheAndOfArray(ARRAY x, int *numberOfElementsOfArray, int newElement){
int position;

position = *numberOfElementsOFArray;   This is the part. :@
x[position] = newElement;

*numberOfElementsOFArray = *numberOfElemenArray + 1;

Post a Reply


Oldest  Newest  Rating
0 Dol Lod · April 7, 2015
numberOfElementsOfArray was passed in as an address for where an integer is stored. 

The *numberOfElementsOfArray dereferences the address specified by the pointer. In other words, it gets the value stored at that location. 

position=*numberOfElementsOfArray sets the value of position to be the value stored at the location pointed at by numberOfElementsOfArray.

In a non-programming context, it could be simplified as someone asking for a pencil. Instead of you directly giving them a pencil, you tell them where a pencil could be found, adding a layer of indirection. They then get the pencil stored at that location. 

I hope this helps. 
0 Alex Carter · April 8, 2015
Thanks, I see this is a very good explanation but I stil don't understand some stuff, I'm a beginner, I'm programmin for a month, and English is not my mother language..Thanks anyway!
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator