함수 매개인자를 어떻게 넘겨줘야되죠? ㅠ.ㅠ
비사벌
질문 내용 :
#include stdio.h
#include stdlib.h
#include string.h
#define swap(x, y, t) ((t) = (x), (x) = (y), (y) = (t))
void bubble_sort(name_ary list[], int cnt)
{
int i, j, temp;
for(i = cnt - 1; i 0; i--){
for(j = 0; j i; j++)
if(list[j] list[j+1])
swap(list[j], list[j+1], temp);
}
}
typedef struct{
char ary[20];
}name_ary;
int main()
{
file *fp;
int cnt = 0;
int i = 0;
name_ary name;
name_ary *otpname;
fp = fopen(data.txt, rt);
if(fp == null){
printf(파일 개방 실패!!!\n);
return 0;
}
while(!feof(fp)){
fscanf(fp, %s, &name.ary);
cnt++;
}
printf(이름의 개수 : %d\n, cnt);
rewind(fp);
otpname = (name_ary*)malloc(sizeof(name_ary) * cnt);
for(i = 0; i cnt; i++){
fscanf(fp, %s, &otpname[i].ary);
}
for(i = 0; i cnt; i++){
printf(%s\n, otpname[i].ary);
}
bubble_sort(otpname[], cnt);
for(i = 0; i cnt; i++){
printf(%s\n, otpname[i].ary);
}
return 0;
}
버블 sort 빨간 부분에 매개인자를 어떻게 넘겨 줘야 되죠?
data.txt에서 한글 이름을 입력 받아 otpname에 집어 넣었는데 bubble_sort(otpname[], cnt); 바로 위
출력 까지는 되는데... 매개인자 값을 어떻게 해야 되나요? ㅠ.ㅠ
-
꽃님이
정말 많은 도움이 되었습니다. 감사합니다. 이제 strcmp 부분만 해결하면 되네요 ㅎ
-
비마중
bubble_sort(otpname, cnt); // 바로 주소를 넘기면 되겠죠
-
한뎃집
void bubble_sort(name_ary list[], int cnt) // name_ary가 선언되기 전에 사용했으니 컴파일러는 못 찾습니다.
{
...
if(list[j] list[j+1]) // 비교대상은 name_ary의 멤버인 ary변수겠죠. 문자열이니 strcmp()로 비교해야죠
SWAP(list[j], list[j+1], temp); // 여기도 name_ary간의 직접적인 대입이 불가능하므로 strcpy()로 ary를 복사해야 합 -
우울녀
fscanf(fp, \%s\