Working on a homework assignment but have been stuck on this problem for the past 2 hours. I basically have a struct called courses. The struct is defined as so:
typedef struct { int crn; char course[STR_SIZE]; char course_number[STR_SIZE]; } courses; I have a dynamically allocated array that contains these structs, called courses_array. I also have another temp array called temp_courses_array. I'm trying to copy the contents of the structs in courses_array to the structs in temp_courses_array by using strcpy(); ie. the course_number element in the i-th position of courses_array should be copied as the course_number element in the i-th position of temp_courses_array. For some reason, whenever I invoke the strcpy() function, the same four elements keep getting copied into temp_courses_array. Here's my code (the output is below the code):
void resize (int **p_size, courses **courses_array) { int i; courses *temp_courses_array; **p_size = **p_size*2; //Initialize the temp array. initialize(p_size, &temp_courses_array); printf("----------------BEGIN EXPECTED OUTPUT----------------\n"); for (i = 0; i < ((**p_size)/2); i++) { printf("%d %s\n", i, ((*courses_array)[i]).course_number); } printf("----------------BEGIN ACTUAL OUTPUT------------------\n"); for (i = 0; i < ((**p_size)/2); i++) { printf("%d %s\n", i, ((*courses_array)[i]).course_number); strcpy((temp_courses_array[i]).course_number, ((*courses_array)[i]).course_number); } free(*courses_array); *courses_array = temp_courses_array; }//resize() I'm at a loss as to why this could be happening. It seems that I'm somehow modifying courses_array by using the strcpy() function... which makes no sense since strcpy() should leave the original array intact. Am I correct in this assumption or am I missing anything? I guess another option would be to copy the entire struct into the temp_courses_array instead of each individual element. I'm not sure how to do that though. If someone could point me in the right direction, that'd be great. Thanks!
Here's the output:
--------------BEGIN EXPECTED OUTPUT---------------- //The first element is the iteration in the loop (i). 0 052 1 123 2 170 3 100B 4 100B 5 106 6 106 7 112 8 136 9 171B 10 171B 11 189C ------------BEGIN ACTUAL OUTPUT--------------- //Notice the repeating elements. 0 052 1 123 2 170 3 100B 4 052 5 123 6 170 7 100B 8 052 9 123 10 170 11 100B EDIT: Here's my initialize() function:
void initialize(int **p_size, courses **courses_array) { *courses_array = (courses *) malloc((**p_size) * sizeof(courses *)); }//initialize()