C언어 포인터 인자 전달 후 처리 방법에 대해 질문드립니다.

storyia의 이미지

캐릭터 포인터를 인자로 받는 함수를 만들어 그 안에서 인자를 변형시킨 다음
메인 함수에서 그 인자를 다른 배열에 할당하는 프로그램을 만들려고 합니다.
그런데 계속 프린트하는 부분에서 run-time 에러가 나는데 이유를 모르겠습니다..
아래는 코드 일부분입니다.

typedef struct stack Stack;
struct stack 
{
	char *arr;
	int len;
	int top;
};
 
int test(Stack *S, char *try)
{
    *try=S->arr[S->top--];
    printf("%c", try); //error가 나는 부분입니다. 어떻게 프린트를 해야하는지 모르겠습니다...
    return 1;
}
 
int main(void)
{
    Stack *S1=(Stack *)malloc(sizeof(Stack));
    S->len=100;
    S->top=-1;
    S->arr=(char *)malloc(sizeof(char)*100);
    char dum, result1[20];
    int k=0, result;
    while(1){
        result=test(S1, &dum);
        if(result==1)
        {
            result1[k]=dum;
            printf("%s\n",dum);//여기서도 역시 오류가 납니다
            k++;
            printf("%s가 할당\n",result1[k]);//출력해보니 할당이 되지 않은 상태였습니다.
        }
    }
    return 0;

Stack에는 위 코드에는 나와있지 않지만 미리 ((di)go)를 집어 넣은 상태이고, top또한 stack에 들어가있는 요소의 수에 맞춰 증가되어 있는 상태입니다. test함수는 괄호가 끝나는 시점까지 계속 진행 되며, )og)id((순으로 나오는 것을 기대하며 작성했습니다.
포인터 인자를 전달 후 그 인자를 다시 출력하려면 혹시 어떻게 작성해야 하는지 알려주실수 있으신가요?

raymundo의 이미지

정확히 어느 시점에 문자를 출력하려는 건지 문자열을 출력하려는 건지는 모르겠습니다만, printf의 포맷 문자열과 대응되는 인자가 서로 일치하지를 않네요.

char 를 출력할 때 %c 를 지정하고 해당 char의 '값'을,
문자열을 출력할 때 %s 를 지정하고 해당 문자열의 첫 글자의 '주소'를 주시면 됩니다.

int test(Stack *S, char *try)
{
    *try=S->arr[S->top--];
    printf("%c", *try); // 방금 뽑아낸 문자 하나를 출력하고 싶다면 try가 아니라 *try
    return 1;
}
 
...
 
printf("%c\n",dum);   // dum 이든 result1[k]든 char 니까 %s가 아니라 %c
printf("%c가 할당\n",result1[k]);

좋은 하루 되세요!

storyia의 이미지

그렇게 고치니깐 바로 출력이 되네요!ㅎㅎ 감사합니다~

세벌의 이미지

Compile 할 때 Error 뿐 아니라 Warning 도 보면 힌트를 얻을 수도 있습니다.
gcc -Wall 로 컴파일 해 보셔요.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.