I'm learning C++ from the book Thinking in C++. There is a paragraph of codes that I don't understand. It's a member function of a struct that holds an array of chars or integers. The add function is supposed to add each element: char or int.
int Stach::add(const void* element){
int startBytes=next*size; //according to the book, next is the next space available and the size is the size of the space
unsigned char* e=(unsigned char*)element;
for(int i=0; i<size;i++)
storage[startBytes+i]=e[i];
next++;
return(next-1);// return index
}
The part I don't understand is what is space, what is the size of the space? The book didn't explain what it is. Also, I'm confused with
unsigned char* e=(unsigned char*)element;
for(int i=0; i<size;i++)
storage[startBytes+i]=e[i];
My understanding of the function is that it copies, say an int, which occupies 4 bytes, byte by byte? Am I understanding it correctly? How do I interpret
unsigned char* e=(unsigned char*)element;
Thanks a lot.