好的 - 請耐心等待冗長的解釋 - 我認爲這對我來說完全是愚蠢的,因爲在c中工作已經幾年了。我遇到了幾個我正在處理的奇怪問題。c傳遞結構數組並刪除一個
FIRST:
有無結構
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct ts{
char *fname;
char *lname;
char *fingers;
char *toes;
};
void delelement(char *, struct ts *);
int i;
int main(int argc, char **argv){
struct ts *ex=(struct ts*)malloc(sizeof(struct ts));
ex[0].fname="joe";
ex[0].lname="bob";
ex[0].fingers="11";
ex[0].toes="9";
ex[1].fname="billy";
ex[1].lname="bronco";
ex[1].fingers="10";
ex[1].toes="10";
ex[2].fname="martha";
ex[2].lname="sue";
ex[2].fingers="12";
ex[2].toes="20";
delelement("billy", ex);
return 0;
}
現在我們到了,我有問題的部分。 現在用於調試我循環並打印出結構數組中的值 - 這是有效的(不要在這個函數中返回一個值 - 我遇到的問題是在我們達到之前)
void delelement(char *delwhat, struct ts *passedex){
//struct ts *tempex=(struct ts*)malloc(sizeof(struct ts));
for(i=0; i<sizeof(passedex)-1; i++){
printf("passedex[%d].fname is %s\n", i, passedex[i].fname);
printf("passedex[%d].lname is %s\n", i, passedex[i].lname);
printf("passedex[%d].fingers is %s\n", i, passedex[i].fingers);
printf("passedex[%d].toes is %s\n", i, passedex[i].toes);
}
return;
}
現在工作正常 - 正確打印信息。
現在讓我們來簡單地刪除註釋和定義結構
void delelement(char *delwhat, struct ts *passedex){
struct ts *tempex=(struct ts*)malloc(sizeof(struct ts));
for(i=0; i<sizeof(passedex)-1; i++){
printf("passedex[%d].fname is %s\n", i, passedex[i].fname);
printf("passedex[%d].lname is %s\n", i, passedex[i].lname);
printf("passedex[%d].fingers is %s\n", i, passedex[i].fingers);
printf("passedex[%d].toes is %s\n", i, passedex[i].toes);
}
return;
}
BOOM - segfault
passedex[0].fname is joe
passedex[0].lname is bob
passedex[0].fingers is 11
passedex[0].toes is 9
passedex[1].fname is billy
Segmentation fault
確定的臨時數組所以我嘗試了不同的方法 - 這有點兒工程
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct ts{
char *fname;
char *lname;
char *fingers;
char *toes;
};
void delelement(char *, struct ts *, struct ts *);
int i;
int main(int argc, char **argv){
struct ts *ex=(struct ts*)malloc(sizeof(struct ts));
struct ts *tempex=(struct ts*)malloc(sizeof(struct ts));
ex[0].fname="joe";
ex[0].lname="bob";
ex[0].fingers="11";
ex[0].toes="9";
ex[1].fname="billy";
ex[1].lname="bronco";
ex[1].fingers="10";
ex[1].toes="10";
ex[2].fname="martha";
ex[2].lname="sue";
ex[2].fingers="12";
ex[2].toes="20";
delelement("billy", ex, tempex);
return 0;
}
void delelement(char *delwhat, struct ts *passedex, struct ts *tempex){
//struct ts *tempex=(struct ts*)malloc(sizeof(struct ts));
for(i=0; i<sizeof(passedex)-1; i++){
printf("passedex[%d].fname is %s\n", i, passedex[i].fname);
printf("passedex[%d].lname is %s\n", i, passedex[i].lname);
printf("passedex[%d].fingers is %s\n", i, passedex[i].fingers);
printf("passedex[%d].toes is %s\n", i, passedex[i].toes);
}
return;
}
WORKS fine... (tempex now defined in main)
passedex[0].fname is joe
passedex[0].lname is bob
passedex[0].fingers is 11
passedex[0].toes is 9
passedex[1].fname is billy
passedex[1].lname is bronco
passedex[1].fingers is 10
passedex[1].toes is 10
passedex[2].fname is martha
passedex[2].lname is sue
passedex[2].fingers is 12
passedex[2].toes is 20
現在,讓我們開始分配值* tempex - 沒有段錯誤,主要定義了tempex
void delelement(char *delwhat, struct ts *passedex, struct ts *tempex){
//struct ts *tempex=(struct ts*)malloc(sizeof(struct ts));
for(i=0; i<sizeof(passedex)-1; i++){
printf("passedex[%d].fname is %s\n", i, passedex[i].fname);
printf("passedex[%d].lname is %s\n", i, passedex[i].lname);
printf("passedex[%d].fingers is %s\n", i, passedex[i].fingers);
printf("passedex[%d].toes is %s\n", i, passedex[i].toes);
tempex[i].fname=passedex[i].fname;
tempex[i].lname=passedex[i].lname;
tempex[i].fingers=passedex[i].fingers;
tempex[i].toes=passedex[i].toes;
}
return;
}
but NOW - weirdness
passedex[0].fname is joe
passedex[0].lname is bob
passedex[0].fingers is 11
passedex[0].toes is 9
passedex[1].fname is billy
passedex[1].lname is bronco
passedex[1].fingers is joe
passedex[1].toes is bob
passedex[2].fname is 11
passedex[2].lname is 9
passedex[2].fingers is billy
passedex[2].toes is bronco
顯然我只是缺少有些愚蠢,或者理解這個錯誤,但現在已經挖出了我無法擺脫的痕跡。任何幫助,將不勝感激。
目標是擁有一個包含char *的結構的動態數組。一旦過去了這個問題,我會在主要的(或者任何地方)有一個我希望刪除其中一個結構的實例。
我要去的是類似...
struct ts* delelement(char *delwhat, struct ts *passedex, struct ts *tempex){
//struct ts *tempex=(struct ts*)malloc(sizeof(struct ts));
for(i=0; i<sizeof(passedex)-1; i++){
printf("passedex[%d].fname is %s\n", i, passedex[i].fname);
printf("passedex[%d].lname is %s\n", i, passedex[i].lname);
printf("passedex[%d].fingers is %s\n", i, passedex[i].fingers);
printf("passedex[%d].toes is %s\n", i, passedex[i].toes);
//load tempex with everything except the one I want to delete
if(!(passedex[i].fname==delwhat)){
tempex[i].fname=passedex[i].fname;
tempex[i].lname=passedex[i].lname;
tempex[i].fingers=passedex[i].fingers;
tempex[i].toes=passedex[i].toes;
}
}
free(passedex); //haven't got here yet - dunno if needed
for(i=0; i<sizeof(passedex)-1; i++){
passedex[i].fname=tempex[i].fname;
passedex[i].lname=tempex[i].lname;
passedex[i].fingers=tempex[i].fingers;
passedex[i].toes=tempex[i].toes;
}
return passedex;
}
所以會產生(或可能)與結構...加載陣列減去一個工作的臨時數組被刪除...重新加載傳遞的結構數組並將其傳回。
一個錯誤可能是您爲1個結構分配內存,但是然後嘗試填充3個元素。而應該是'struct ts * ex =(struct ts *)malloc(3 * sizeof(struct ts));' – 2011-03-29 16:04:50
相同的用戶重複:http://stackoverflow.com/questions/5474925/c-linux-pasing-array -of-結構到刪除元素 – Muggen 2011-03-29 16:15:35