C언어 문자열 정렬중 strcmp에서 warning이 뜨네요... 도와주세요ㅠㅠ

lhs8421478의 이미지


 
void select_sort_string()
{
    char arr[6][20] = {"joy", "root", "super", "sound", "he", "program"};
    int num1;
    int num2;
    char *select;
    char *min;
    char tmp[20];
 
    memset(tmp, 0x00, sizeof(20));
    for (num1 = 0; num1 < 5; num1++) {
        select = arr[num1];
        min = arr[num1];
        for (num2 = num1 + 1; num2 < 6; num2++) {
            if (strcmp(select[num1], select[num2]) > 0) {
                min = &select[num2];
            }
        }
    }
}

문자열 정렬을 하려고 코딩을 하려고 합니다.

2차원 배열을 이용해서 단어들을 배열에 저장하고

포인터를 이용해서 배열의 내용을 선택정렬을 하려고 했었습니다.

strcmp에서 밑에와 같은 warning이 뜨네요...

select_sort.c: In function ‘select_sort_string’:
select_sort.c:84:4: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [enabled by default]
/usr/include/string.h:143:12: note: expected ‘const char *’ but argument is of type ‘char’
select_sort.c:84:4: warning: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast [enabled by default]
/usr/include/string.h:143:12: note: expected ‘const char *’ but argument is of type ‘char’

도움 부탁 드립니다.

raymundo의 이미지

비교해야 할 건 arr[num1]과 arr[num2]겠지요.

좋은 하루 되세요!

lhs8421478의 이미지

넵 비교해야할것은 arr[num1]과 arr[num2]이지만...

포인터를 이용해서 해야되서요...
포인터가 가르키고 있는곳의 값을 비교해야 하는것인데...
strcmp가 아니면 다른걸 써야 할까요?

raymundo의 이미지

아, 정렬하는 중이고 min 값을 찾아야 하니까... min 과 비교를 해야겠군요.

어쨌거나 select 는 지금 char * 입니다. 그러니 select[숫자] 는 이건 문자열이 아니라 select가 가리키는 곳에서 숫자만큼 떨어진 곳에 있는 char 하나죠.

strcmp 에 만일 넣는다면 그냥 select 라고만 넣으셔야 하고...

정렬하시려는 의도대로라면 다음과 같이...

    for (num1 = 0; num1 < 5; num1++) {
        select = arr[num1];
        min = arr[num1];
        for (num2 = num1 + 1; num2 < 6; num2++) {
            if (strcmp(min, arr[num2]) > 0) {
                min = arr[num2];
            }
        }
 
        // 사실 select 는 아예 전혀 쓸 필요가 없고, 이 아래 세 줄에서
        // select 대신 arr[num1]을 쓰면 되죠
        strcpy(tmp, select);
        strcpy(select, min);
        strcpy(min, tmp);
    }

좋은 하루 되세요!

lhs8421478의 이미지

답변 감사합니다 ^^

지금 포인터만 이용해서 하는게 목표라서 저렇게 해보았습니다 ㅠㅠ

근데 잘 안되네요 ㅠㅠ

익명 사용자의 이미지

strcmp는 주소값을 인자로 받습니다. select[num1]은 주소값인가요? select 는 주소값인가요? arr[num1]은 주소값인가요? 생각해보세요.

cats96의 이미지

더블배열을 포인터로 받을려면

char * select[6];

select[0] = &arr[0];
select[1] = &arr[1];
select[2] = &arr[2];
select[3] = &arr[3];
select[4] = &arr[4];
select[5] = &arr[5];

이렇게 6개 다 받아야합니다

만약 select = arr[0]; 받는다면

스트링 출력시
select[0] = "joy"
select[1] = "oy"
select[2] = "y"
.
.
.

이렇게 나오겠죠

lhs8421478의 이미지

넵 이차원 배열을 그냥 포인트에 담을려던것이 실수였네요....

현재 계속 뜯어 고치고는 있습니다.

void select_sort_string(char *arr)
{
	int num1;
	int num2;
	char (*select)[20];
	char (*min)[20];
	char tmp[20];
 
	memset(tmp, 0x00, sizeof(20));
	select = &arr;
	for (num1 = 0; num1 < 5 ; num1++) {
		min = &select[num1];
		for (num2 = num1 + 1; num2 < 6; num2++) {
			if (strcmp(min[num1], select[num2]) < 0) {
				min = &select[num2];
				printf("%s\n", min);			
			}
		}
	}
}

이런식으로 바꾸었는데 아직 strcmp에서 걸리네요....

제 생각에는 처음에 출력되야할 글자는 joy가 제일 먼저 출력 되야 할텐데.. root가 출력되고 있네요....

문제점을 점차 더 찾아가봐야겠어요 ㅠㅠ

조언 감사합니다 ^^

cats96의 이미지

심심해서 문자열 선택정렬 함수 만들어봤어요;;

void select_sort_string()
{
char arr[6][20] = {"joy", "root", "super", "sound", "he", "program"};
int num1;
int num2;
char select[20];
char tmp[20];
int i = 0;

memset(tmp, 0x00, sizeof(char)*20);

for (num1 = 0; num1 < 5; num1++)
{
for (num2 = num1+1;num2 < 6; num2++)
{
if (strlen((char*)&arr[num1]) > strlen((char*)&arr[num2]))
{
strcpy(tmp,(char*)&arr[num1]);
strcpy((char*)&arr[num1],(char*)&arr[num2]);
strcpy((char*)&arr[num2],tmp);
}
}
}

for(i = 0;i<6;i++)
printf("%s\n",arr[i]);

}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.