I have two structs pointers to pointers
typedef struct Square {
...
...
}Square;
Square **s1; //Representing 2D array of say, 100*100
Square **s2; //Representing 2D array of say, 200*200
Both are allocated on the heap using malloc().
I have s1 initialized with certain values and s2 initialized completely with the default values.
Basically I need to resize s1 to the size of s2 while maintaining its (s1) values, and the ‘added’ values would be just as they were in s2 – the default value.
I wrote this question memcpy() from smaller array to larger one but apparently I’m confusing between arrays and pointers/
My question is, how to implement this resizing of s1 to the size of s2. I don’t have to keep the original pointer. I can copy s1 to s2 and return s2 if that’s a better way
I hope I explained what I’m after properly.
Thanks!
Two dimensional arrays are laid out in memory sequentially: row1 row2 row3 etc.
memcpy does a linear copy from one memory location to another.
So to achieve what you need:
a) Create a new array
b) Copy s2 into it
c) Copy stuff from s1, row by row into new
http://www.fredosaurus.com/notes-cpp/arrayptr/23two-dim-array-memory-layout.html