/* Function |insert| inserts block of ints |toInsert|, having |dim2| elements, i
ID: 3553954 • Letter: #
Question
/* Function |insert| inserts block of ints |toInsert|, having |dim2| elements, into the
// middle of block of ints |blk|, which has |dim1| elements. The inserted block starts at
// offset |offset|. Data in |blk| are moved down to make room; no data is lost.
// |insert| makes a larger block to accommodate the inserted data, and returns a pointer
// to this larger block, which the caller is expected to use instead of |blk|.
*/
It does not print the correct tmp variable I do not why
int *insert(int *blk, int *toInsert, int dim1, int dim2, int offset) {
int *tmp = calloc(dim1+dim2, sizeof(int));
int i = 0;
do{
tmp[i++] = *blk++;
printf("%d", tmp[i]);
}while(offset--);
return tmp;
}
Explanation / Answer
int *insert(int *blk, int *toInsert, int dim1, int dim2, int offset) { int *tmp = calloc(dim1+dim2, sizeof(int)); memcpy(tmp, blk, offset*sizeof(int)); memcpy(tmp+offset, toInsert, dim2*sizeof(int)); memcpy(tmp+offset+dim2, blk+offset, offset*sizeof(int)); //free(blk); return tmp; }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.