I read a book, which gives example of .h file, that equal to interface of queue in java:
void InitQueue(Queue* pQueue, int iSize);
bool Insert(Queue* pQueue, const Item* pItem);
bool Extract(Queue* pQueue, Item** pItem);
bool GetFirst(const Queue* pQueue, Item** pItem);
bool IsFull(const Queue* pQueue);
bool IsEmpty(const Queue* pQueue);
I didn’t understand two things:
- Why in
Extract, GetFirstThe second argument is of typeItem**and notItem*? What is mean when we wrtoe such a thing? - Why in some functions (
IsFull, IsEmpty, ..) we get as argumentconst Queue*and not simplyQueue*?
IsFull() and IsEmpty() take const arguments because it implies that they will not change the Queue object; it is constant, and will not be modified.
Extract and GetFirst use ** because of this:
If I were to pass c into a function:
The pointer I just passed in was itself modified; **c is now pointing to global_var instead of a.
The reason you pass these pointers into getfirst and extract is because those are “getter” functions – you want them to return a pointer to the data. So they need to be able to return a pointer, and the method these employ is to pass a pointer to a pointer, like in the above example, so they can modify what you pass them to point to the correct element.