C언어질문입니다.

studioego의 이미지

제가 C언어 책을 보다가 의문가는 사항이 있어서 질문합니다.

char *strcpy(char *s, const char *t)
{
   char *p = s;
   
   while (*s++ = *t++)
   ;
   return p;
}

여기서
char *p = s;

char *p;
p = s;

와 같다고 하네요.
이해가 잘 안갑니다.

추가로 제 생각에는
p=s; 쓰는 것 보다는 p=&s;로 쓰는 것이 맞는 것 같은데...

chadr의 이미지

sungdh86 wrote:
제가 C언어 책을 보다가 의문가는 사항이 있어서 질문합니다.

char *strcpy(char *s, const char *t)
{
   char *p = s;
   
   while (*s++ = *t++)
   ;
   return p;
}

여기서
char *p = s;

char *p;
p = s;

와 같다고 하네요.
이해가 잘 안갑니다.

추가로 제 생각에는
p=s; 쓰는 것 보다는 p=&s;로 쓰는 것이 맞는 것 같은데...

s자체가 포인터이므로 p=s가 맞습니다..

p = &s로 하면 이중포인터를 의미하므로(s라는 포인터의 포인터) 형이 안맞다고 컴파일 에러가 날것입니다.

포인터에 대한 설명을 더 자세히 봐보세요..

-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.

GunSmoke의 이미지

sungdh86 wrote:
char *strcpy(char *s, const char *t)
{
   char *p = s;
   
   while (*s++ = *t++)
   ;
   return p;
}

여기서
char *p = s;

char *p;
p = s;

와 같다고 하네요.

선언과 동시에 초기화를 한 것, 선언을 한 후에 대상체에 값을 할당한 차이가 있을 뿐입니다. 본질적인 의미는 동일합니다.

大逆戰

소맛라면의 이미지

변수 s type은 char*형, 변수 p의 type도 char*
따라서 p=s가 맞습니다.
포인터 변수 p에 포인터 변수s의 주소값을 넘기는 것은
윗분 말처럼 이중포인터겠죠~

무엇을 위해 사는가..

Fe.head의 이미지

char *strcpy(char *s, const char *t)
{
   char *p = s;
   char *p1 = &s;

(char *) 로 묶는다.

(char *)strcpy((char *)s, const (char *)t)
{
   (char *)p = s;
   (char *)p1 = &s;

(char *) == int 로 가정하면

int strcpy(int s, const int t)
{
   int p = s;
   int p1 = &s;

모든 변수의 형을 int형화 시켜보면 좀 쉽게 볼수 있답니다. :)
심지어 struct, class도 ..

고작 블로킹 하나, 고작 25점 중에 1점, 고작 부활동
"만약 그 순간이 온다면 그때가 네가 배구에 빠지는 순간이야"

소맛라면의 이미지

Quote:
모든 변수의 형을 int형화 시켜보면 좀 쉽게 볼수 있답니다. :)
심지어 struct, class도 ..

정말 괜찮은 생각입니다..잘..배우고 가요~ㅋㅋ

무엇을 위해 사는가..

댓글 달기

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