我有一个struct
:复制串入结构ç
typedef struct myStruct {
char** a;
char** b;
} myStruct;
我试图从stdin
读取和初始化的myStruct*
int main() {
int maxlength = 60 + 1;
int arraySize = 2;
myStruct** myArray = (myStruct*) malloc(sizeof(myStruct*) * arraySize);
int runningIndex = 0;
while(1) {
char* aline = (char*) malloc(maxlength);
int status = getline(&aline, &maxlength, stdin);
if(status == -1)
break;
char* bline = (char*) malloc(maxlength);
getline(&bline, &maxlength, stdin);
if(runningIndex == arraySize) {
arraySize *= 2;
myArray = realloc(myArray, sizeof(myStruct*) * arraySize);
}
myArray[runningIndex] = (myStruct*) malloc(sizeof(myStruct*));
myArray[runningIndex]->a = &aline;
myArray[runningIndex]->a = &bline;
runningIndex++;
}
for(int i = 0; i < runningIndex; i++) {
printf("outside the loop at index %d, a is %s and b is %s", i, *myArray[i]->a, *myArray[i]->b);
}
}
一个数组,我做了while
在几printf
确认每个myStruct
成功创建与从stdin
字符串。但是,在循环之外,所有存储的值似乎都消失了。我正在考虑范围,但无法弄清楚原因。有人可以解释我应该如何正确地做到这一点?谢谢!
[不投malloc'的'结果。(http://stackoverflow.com/questions/605845/do- i-cast-of-malloc的结果)你在哪里离开'while循环? – pzaenger
我明白了。谢谢你教我。对不起,已编辑。 – Ra1nWarden
源和dest == a和b? –