[완]구조체 입력에 대한 error / 조언좀 부탁드려요

dj3218의 이미지

c 다시 배우고 있는 초보입니다.
채팅 프로그램을 만들어보고 있습니다.

typedef struct {
int client_id;
char client_ip;
char client_cnttime;
}clientinfo;
clientinfo clntInfo[10];

int clnt_id

위쪽에 구조체 만들었고
중간에는 소켓 만들고 bind listen 이런거 나오다가
반복문 안에다가

clntInfo[clnt_id].client_id = clnt_id;
clntInfo[clnt_id].client_ip = (char*)inet_ntoa(clnt_addr.sin_addr);

이렇게 넣었습니다 id부분은 정상적으로 출력되지만 ip부분이 에러로 아래처럼 나오는데
어떻게 해결해야 할지 모르겠습니다. 지식공유좀 부탁드리겠습니다.

incompatible types when assigning to type 'char [50]' from type 'char *'

처음에는 char* 없이 해봤다가 다른크기 정수 캐스트 warning 떠서 수정하고 여러가지 넣어봤는데
도통 머리속에 떠오르질 않습니다...

clntInfo[].client_ip 에 ip를 문자로 넣고 싶습니다..

세벌의 이미지

raymundo의 이미지

에러 메시지에 따르면 코드에서 client_ip 는 char 형이 아니라 char 의 배열 형으로 선언되어 있을 것 같은데요?

암튼 inet_ntoa 매뉴얼 페이지를 보시면 이게 반환하는 공간은 정적으로 할당된 공간이고 다음번에 이 함수가 (다른 인자를 전달받아) 호출되면 그 공간에 다른 문자열이 쓰여지게 됩니다. 그러니 호출 후에 결과를 복사해서 쓰셔야 합니다.

typedef struct {
   int client_id;
   char client_ip[16]; // xxx.xxx.xxx.xxx 형식이니 최대 15자리 + 마지막 널 캐릭터
   char client_cnttime;
}clientinfo;
 
// ...
strcpy( clntInfo[clnt_id].client_ip , inet_ntoa(...) );   // char배열에 문자열 복사. 이왕이면 strncpy 를?

좋은 하루 되세요!

dj3218의 이미지

조언을 보고 실행해보니 제가 원하던 대로 출력이 됩니다. 시간쪽도 이것과 비슷한 문제가 있었는데 이걸 토대로 해보겠습니다.

댓글 달기

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