간단한 도메인 출력 프로그램입니다.

seye2의 이미지

이 프로그램은 제가 아이피의 C클래스 에서 netid부분의 주소를 입력하면
해당 네트워크에 속한 모든 호스트의 도메인 네임을 출력하는 프로그램입니다. 즉, 192.123.58.을 입력하면 192.123.58.1부터 192.123.58.254사이의 IP
주소를 갖는 모든 호스트의 도메인 네임을 출력하는 프로그램인데요.
제가 표시해둔곳 for문안에서의 strcpy(haddr,temp)부분이 실행이되면
세그멘테이션 오류가 나더라구요. 근데 이부분이 되야
strcat으로 붙여진 내용들이 재초기화가 되는 중요한 부분인데
왜냐면 strcat으로 붙여진 내용들은 문자열들이 계속 뒤에 붙더군요.
휴.....숙제이기도 하지만 해보고 싶은데 아직 C언어에 대한 문법적
한계는 쉽게 풀기가 어렵네요. 고수님들 도와주세요~~.

int main(int argc, char **argv) {
char *haddr, *buff, *temp;
struct hostent *myhost;
struct in_addr host_ip;
int i;

if( argc < 2 ) {
printf("사용법 : %s IP 주소(dotted decimal) \n", argv[0]);
exit(0);
}

temp = argv[1];

for( i = 1; i <= 254; i++) {
strcpy(haddr,temp); //세그멘테이션 오류가 나는곳
sprintf(buff, "%d", i);
strcat(haddr,buff);
printf("%s\n", haddr);
host_ip.s_addr = inet_addr(haddr);

printf("IP Address (hexa) 0x%x\n", host_ip.s_addr);

myhost = gethostbyaddr((char *)&(host_ip.s_addr), sizeof(host_ip.s_addr), AF_INET);

if(myhost == NULL) {
printf("Error at gethostbyaddr()\n");
exit(0);
}

printf("official host name : \t\t%s\n", myhost->h_name);
}

return 0;
}

정태영의 이미지

int main(int argc, char **argv) { 
char *haddr, *buff, *temp; 
.
.
.
temp = argv[1]; 

for( i = 1; i <= 254; i++) { 
strcpy(haddr,temp); //세그멘테이션 오류가 나는곳

haddr은.. 어떤 메모리 공간을 가리키고 있는 포인터일뿐입니다..

이 포인터가 초기화조차 되지 않았기 때문에..
어디를 가리키고 있을지 알 수 없는 상태이기 때문에.. 현재
이 프로그램에 할당되지 않은 메모리를 가리키고 있을 확률이 아주 높습니다

그래서 세그멘테이션 폴트가 뜨게 된거 겠죠 ;)

char* haddr을..
char haddr[1024]; 정도로 바꿔보세요..

오랫동안 꿈을 그리는 사람은 그 꿈을 닮아간다...

http://mytears.org ~(~_~)~
나 한줄기 바람처럼..

댓글 달기

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