(char *)&buf1 이 의미하는 것은?

익명 사용자의 이미지

안녕하세요. c초보 입니다.

send(sockClient, (char *)&buf2, sizeof(buf2), 0)

간단한 소켓 프로그래밍을 하고 있는데 함수에 넘겨주는 인자가 위와 같습니
다. (절대 send 함수 사용법을 물어보는 게 아니에요)

(char *)&buf1 <- 이 부분이 이해가 안갑니다. buf1의 주소를 문자형 포인터
로 캐스팅 하는건가요? 즉, send 함수는 전송할 값의 어드레스를 문자열로 받
나요? -_-?

여기서 buf1은 아래와 같은 구조체 입니다.

struct stcBuf2{
short shSize;
char temp[31];
short shRecv;
};

고수님 보시기에 허탈한 질문일 수도 있겠지만 잘 좀 부탁 드릴께요.

멍청해서 죄송합니다. ㅠ.ㅠ

익명 사용자의 이미지

답변을 이미 알고 계신 것 같네요.
형변환(보통 캐스팅이라고 하죠.)하는 것이 맞습니다.
즉, send의 함수형태(다른 함수들도 마찬가지 이지만)에서 두번째 인자는
char *로 지정이 되어있죠. 어떠한 데이터이든지 char *로 보내야만 합니다.
심지어 그것이 숫자형일때도...
보기에서는 구조체를 char*로 형변환을 시켰는데 send라는 함수는 이것이
구조체인지 문자형인지를 구분하지 못합니다. 즉, 그렇게 정해주는건 프로그래
머가
해주어야 하는거죠. 그러면 컴파일러에서 실행파일을 만들때 형변환을 시켜주

원하는 형태로 보내주는 겁니다.
음... 그런데 제가 보통 사용하는 방법은

typedef struct ABC {
int a;
char b [2];
int c;
} abc;

이렇게 형이 정의 되어있을 때

abc a;
...

send (sock, (abc *)&a, sizeof (abc), 0);
...

이렇게 char *로 캐스팅을 하지 않고 구조체 자체로 캐스팅을 합니다.
물론 받는 쪽도 똑같은 형태의 구조체를 받게하죠.
저도 자세하게 알고 사용하는 것이 아니지만 구조체형태로 보내고 받는게
프로그램을 직관적으로 작성할 수 있고 에러의 소지도 줄일 수 있다고
봅니다.

댓글 달기

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