아하
생활

생활꿀팁

느긋한쇠오리233
느긋한쇠오리233

문자열 입력 시 에러가 뜹니다ㅠㅠ

Code 빌드 후 실행시키고 문자를 입력하면 3번째부터 에러가 뜹니다.

동적할당 부분을 어떻게 수정해야 하나요?

#include "stdafx.h" #include "stdio.h" #include "stdlib.h" #include "string.h" #define str 80 struct profile { char name[20]; int age; double height; char *intro; }; int main() { struct profile yuni; strcpys(yuni.name, "나유니"); yuni.age = 17; yuni.height = 164.5; yuni.intro = (char)malloc(sizeof(char)*str); printf("자기소개 :"); getss(yuni.intro,sizeof(yuni.intro)); printf("이름 : %s\n", yuni.name); printf("나이 : %d\n", yuni.age); printf("키 : %.1lf\n", yuni.height); printf("자기소개 : %s\n", yuni.intro); free(yuni.intro); return 0; }
55글자 더 채워주세요.
1개의 답변이 있어요!
  • 프알못
    프알못

    sizeof는 컴파일 시점에, 그 타입으로 계산됩니다.

    getss(yuni.intro,sizeof(yuni.intro));

    여기서 sizeof(yuni.intro)는 sizeof(char *)로 4나 8밖에 안 됩니다.

    gets_s(yuni.intro,sizeof(char) * str);

    이렇게 바꿔야 될 듯 합니다.