char를 가리키는 포인터들의 배열이지만, 그 포인터들은 유효한 메모리를 가리키지 않습니다.
먼저 포인터들이 쓸 저장 공간을 할당하세요:
int main(void)
int pos;
char *labels[8];
for (pos=0; pos<8; ++pos) {
labels[pos]=malloc(LABELCAP);
scanf("%17s",labels[pos]);
}
for (pos=0; pos<8; ++pos) {
printf("%s\n",labels[pos]);
free(labels[pos]);
}
return 0;
}
또 다른 방법은
int main(void) {
int pos;
char labels[8][LABELCAP];
for (pos=0; pos<8; ++pos) {
scanf("%17s",labels[pos]);
}
for (pos=0; pos<8; ++pos) {
printf("%s\n",labels[pos]);
}
return 0;
}
영어에서 번역·원문 보기Those pointers form an array pointing to char, but they aren't referncing any valid memmory.
First allocate storage for those pointers:
int main(void)
int pos;
char *labels[8];
for (pos=0; pos<8; ++pos) {
labels[pos]=malloc(LABELCAP);
scanf("%17s",labels[pos]);
}
for (pos=0; pos<8; ++pos) {
printf("%s\n",labels[pos]);
free(labels[pos]);
}
return 0;
}
another option is
int main(void) {
int pos;
char labels[8][LABELCAP];
for (pos=0; pos<8; ++pos) {
scanf("%17s",labels[pos]);
}
for (pos=0; pos<8; ++pos) {
printf("%s\n",labels[pos]);
}
return 0;
}