구조체 char 포인터변수ㅠㅠ

wkdend의 이미지

구조체에서 배열 선언하고 실행시키면 되는데
char형 포인터 선언한 다음 malloc으로 메모리 동적할당 하고
실행하면 계속 런타임 오류가 나네요ㅜㅜ
뭐가 잘못된걸까요? 너무 오랜만에 해서 잘모르겠어요ㅠㅠ

wkdend의 이미지

#include
#include
#include
struct phonebook_t
{ char *s; int num;} list;
main()
{ int i; 
char *search; 
char test[20];
// for(i=0;i<3;i++)
// {  list.s=(char *)malloc(30);  
search=(char *)malloc(20);  
printf("s의 sizeof=%d\n", sizeof(*list.s)); 
 printf("num의 sizeof=%d\n", sizeof(list.num));  
printf("search의 sizeof=%d\n", sizeof(*search));  
printf("test의 sizeof=%d\n", sizeof(test));// }  
for(i=0;i<1;i++) 
{  printf("사람 %d의 이름 및 전화번호를 입력하세요 : ", i+1);
  scanf("%s %d", &list.s, &list.num); 
 fflush(stdin); } 
 printf("s2의 sizeof=%d\n", sizeof(*list.s));
 printf("찾고자 하는 사람의 이름을 입력하세요 : "); 
scanf("%s", search); 
fflush(stdin);
 printf("search=%s\n", search);
 printf("list의 주소=%u\n", &list.s); 
printf("list=%s\n", *list.s);
// printf("%s\n", list[1].s);
// printf("%s\n", list[2].s);
 for(i=0;i<1;i++)
 {  if(!strcmp(search,list.s)) 
 {   printf("찾으신 %s의 전화번호는 %d입니다\n", list.s, list.num);
   break;  } }}

raymundo의 이미지

코드는 code ~ /code 태그로 둘러싸주세요. (좌우에 부등호)

// 이렇게 나오게요

1) list.s 가 포인터만 있고 정작 그 포인터가 가리키는 공간이 없음.

2) scanf 에서 %s 에 해당하는 인자는 &list.s 가 아니고 list.s 여야 함

3) 마지막 printf 에서도 *list.s 가 아니라 list.s 여야 함

정도가 보이는군요.

그리고 char * 변수에다가 * 붙이고 sizeof 해봤자 (sizeof(*search) 등) char 타입의 길이인 1밖에 안 나오지,
malloc()으로 할당받은 크기가 나오지는 않습니다. 아마 의도가 그건 것 같은데.

좋은 하루 되세요!

익명 사용자의 이미지

되네요!!!!
감사합니다^^

댓글 달기

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