我有一個結構定義爲:複製結構數組元素不起作用? Ç
subscriber database[num_of_subscribers]
我寫應該交換兩個元素中的一個功能:
typedef struct
{
char first_name[11];
char last_name[21];
char city_code[3];
char zip_code[5];
char area_code[4];
char phone_num[8];
} subscriber;
我根據來自控制檯輸入創建這些結構的陣列array:
void swap_cells(subscriber dbase[],int index1,int index2)
{
subscriber temp;
memcpy(&temp,&dbase[index1],sizeof(temp));
memcpy(&dbase[index1],&dbase[index2],sizeof(temp));
memcpy(&dbase[index2],&temp,sizeof(temp));
}
它不工作,因爲我認爲它......任何想法爲什麼?
什麼行爲,你看到的,讓你覺得它不工作? –
@RSahu - 我試圖複製的第二個單元格顯示已損壞。 – user34920
我沒有看到有效數據和有效索引的代碼有任何問題。我對指數的幾個不同組合進行交換代碼時沒有任何問題。我懷疑你已經超越了設置數據的界限,或者你正在使用無界限索引。 –