what your talking about is called a texture atlas. basically packing a bunch of images into a single image, and then using a meta-data format(xml, yaml, etc) to describe where each asset is in the texture. ideally, you should not be constructing this real time, and prepare the atlas offline(this can also potential save you space, as you reduce alot of overhead associated with multiple images, by placing them into one image).
however if you want to do it at load time, basically an image is a series of pixels composed of a number of components(rgb = 3 components, red, green, blue, and rgba = 4 components, red, green, blue, alpha). each component is generally only a single byte of data, so they have 0-255 possible values. (note: this is not universal, and alot of packing techniques can and will use different bit sizes per component, but for simplicity sake we'll only talk about rgb 8 bit images). Now then, in order to pack these images together, the simplest method would be to go through each image, and get their dimensions, and track the largest width, and height of images. once you have this, you can allocate an array which is Width*Images*PixelSize by Height. then you simply copy each image pixels into each spot in the grid. of course this method isn't very power of 2 textures friendly, so i'd advise doing further research on texture atlases to get a better grasp of how you can do it.
something like the following:
int LargestWidth = 0;
int LargestHeight = 0;
for(int i=0;i<NbrImages;i++){ //we are assuming each image has the same pixel size!
LargestWidth = max(Image[i].Width, LargestWidth);
LargestHeight = max(Image[i].Height, LargestHeight);
}
int Stride = LargestWidth*NbrImages*PixelSize; //stride is how far we have to travel to get to the next row.
int Width = LargestWidth*NbrImages; //actual width of the new image.
int Height = LargestHeight; //actual height of the new image.
char *Texels = new char[Stride*Height]; //Texels is another word very often used when talking about pixels.
for(int i=0;i<NbrImages;i++){
int Loc = LargestWidth*i*PixelSize; //First pixel location for this image.
for(int y=0;y<Image[i].Height;y++){
memcpy(&Texels[Loc+y*Stride], &Image[i].Texels[y*Image[i].Stride], sizeof(char)*Image[i].Stride); //Now we copy into our large image, 1 row at a time!
}
}
Image *I = new Image(Width, Height, Stride, Texels);
this is obviously just psedo-code to demonstrate the concept, again texture atlas's are something you should be making outside of the application, and not at load time.