[질문] 포인터 초보...char * 을 리턴하는 함수는 어떻게 받아야

hurryon의 이미지

[hurryon@note 0508]$ cat 2.c
#include <stdio.h>

char *arr_print(char *source, char *des);

char *arr_print(char *source, char *des)
{
        des = source;

        printf("%s\n", source);
        printf("%s\n", des);

        return des;
}

int main(void)
{
        char *source = "hurryon";
        char des[50];

/*      des = arr_print(source, des); */

        printf("%s\n", source);
/*      printf("%s\n", des); */
        printf("%s\n", arr_print(source, des));

        return(0);
}
[hurryon@note 0508]$

주석을 처리한 부분을 풀어도 될꺼 같은데 제대로 되지 않아서 주석 처리
했습니다. 저렇게 char * 함수을 받는것이 아닌지요? 그냥 printf 문에
바로 쓰니 되긴하지만요.

pynoos의 이미지

Quote:

char des[50];
des = arr_print(source, des);

배열과 pointer는 속성상 상당히 일치하기 때문에,

pointer를 pointer에 대입하는 꼴로 생각하면,

des 값이 arr_print 가 return 하는 값으로 대치 될 뿐입니다.

원하는 기능은 des가 가리키는 곳에 arr_print 가 넘기는 pointer가

가리키는 내용입니다. 그렇게 하려면, strncpy나 memcpy를 쓰시는 것이

맞는 방법입니다.

체스맨의 이미지

       char des[50];
       des = arr_print(source, des);

des 는 포인터 상수 입니다. 이러한 대입은 마치,
1 = a;
이렇게 쓴 것과 마찬가지입니다. 대개 좌측은
l-value 가 필요하다고 나올겁니다.

char* s = arr_print(source,des) 이렇게 하는 건 문제 없습니다.
strcpy 를 쓰는 여부는 님께서 무엇을 의도하느냐에 따라
달라질 것이라 봅니다.

Orion Project : http://orionids.org

xfmulder의 이미지

"배열명은 (배열의 첫번째 요소를 가르키는)포인터 상수 입니다"

즉, char des[5];
에서 des == &des[0] 을 의미하는 포인터 상수 입니다.

상수이기 때문에 대입이 안됩니다. (lvalue 로 쓸수 없습니다.)

예를들면 int i,j = 10, *pi ; 가 있다고 보면
pi = &j ; <--- 이건 당연히 됩니다. 포인터 변수니까.
&i = &j ; <--- 이건 안됩니다. i의 주소는 이미 상수니까.
lvalue 로 쓸수 없습니다.
memcpy(&i,&j, sizeof(j)); <-- 이것도 되겠지요.

이상과 같이 des 는 &des[0] 이고 위의 &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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.