|
춤추는대수사선 님이 쓰신 글 :
: 문제는
: char chtemp[]="Let me say those words as hently as I can"; 의 문자열을
: 동적 메모리에 보관하고 이를 출력하는 프로그램을 작성하여 보세요.
:
: ---------------제가 풀어볼려고했던 답--------------------
: #include <stdio.h>
: #include <stdlib.h>
:
: void main() {
:
: char chtemp[]="Let me say those words as hently as I can";
char chtemp[]="Let me say those words as hently as I can"; 의 경우 chtemp 는 문자열상수.
굳이 chtemp 에 다른 메모리를 할당 하려면 char *chtemp = "..."; 로 사용을 하셔야 하죠.
: chtemp = (char *)malloc(50); //이부분//
chtemp 에 새로운 메모리를 할당 하셨으므로, 위의 "Let me say those words as hently as I can";
문자열은 미아가 되겠네요.
: printf("%s \n",*chtemp);
위 부분에서 Access violation 이 발생할 겁니다. *chtemp 는 문자 'L' 을 가리키는 것이고,
이를 "%s" 로 출력하고자 하면 'L' 값을 포인터로 번역해서 전혀 엉뚱한 곳의 내용을 출력하려고
할것 같네요.
% 정리하면 위 말씀하신 부분을 새로운 변수에 할당하도록 변경하셔야 합니다.
char *a;
a = (char *)malloc(50);
: free(chtemp);
: }
: -------------------------------------------------------------
:
: //이부분// 에서 에러가 나더군요!
: 에러메세지는(터보C++3.1 for Windows) Lvalue required 이고,
: 비주얼C++은
: error C2440: '=' : cannot convert from 'char *' to 'char [42]'
: There are no conversions to array types, although there are conversions to references or pointers to arrays
: Error executing cl.exe.
: 이렇게 나오네요
:
: 뭐가 문제일까요.......ㅡㅡa
:
: 김민식님 감사합니다.
|