Mình có code sau.
// Assignment.cpp
struct position {
int x;//row
int y;//column
};
void print_position(struct position pos) {
printf("(%d, %d) ", pos.x, pos.y);
}
void print_lst_position(struct position* lst_pos, int size) {
int i = 0;
while(i < size) {
print_position(lst_pos[i]);
i++;
}
}
struct position* generate_list(int** board_state, int *size) {
struct position lst[4];
*size = 4;
//assigning values
lst[0].x = 2;
lst[0].y = 3;
lst[1].x = 3;
lst[1].y = 2;
lst[2].x = 4;
lst[2].y = 5;
lst[3].x = 5;
lst[3].y = 4;
return lst;
}
//main.cpp
int main() {
struct position *test;
int ** a;
int size;
int i;
a = (int**)malloc(8 * sizeof(int*));
for ( i = 0; i < 8; ++i)
a[i] = (int*) malloc(8 * sizeof(int)) ;
test = generate_list(a, &size);
print_lst_position(test, size);
_getch();
return 0;
}
Cho mình hỏi sao khi chạy nó lại xuất ra các giá trị rác.