C언어 질문입니다!! (calloc, 오류찾기..)
-
게시물 수정 , 삭제는 로그인 필요
주석으로 코드에 맞는 문제를 줍니다.
/* repeat.c - repeat a given string n times
* Compile with CFLAGS="-pedantic –std=c11" make repeat
* Use with e.g. ./repeat 20 hello
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
long count = strtol(argv[1], NULL, 10);
char *word = argv[2];
size_t length = strlen(word);
char *buffer = calloc(count, length + 1); // length+1 so we can add a newline
for (size_t i = 0; i < count; i++) {
char *destination = &buffer[i * (length + 1)];
strcpy(destination, word);
buffer[i * (length + 1) + length] = 10;
}
buffer[count * (length + 1)] = '\0';
printf("%s", buffer);
free(buffer);
}
1. 프로그램에 count inpu 으로 –1 이 주어지면, calloc 으로 얼마나 많은 메모리가 할당 되어야 하죠??
2. 위 코드에서 오류가 4개 있다고 하는데, 뭐뭐 있나요??
주석으로 코드에 맞는 문제를 줍니다.
/* repeat.c - repeat a given string n times
* Compile with CFLAGS="-pedantic –std=c11" make repeat
* Use with e.g. ./repeat 20 hello
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
long count = strtol(argv[1], NULL, 10);
char *word = argv[2];
size_t length = strlen(word);
char *buffer = calloc(count, length + 1); // length+1 so we can add a newline
for (size_t i = 0; i < count; i++) {
char *destination = &buffer[i * (length + 1)];
strcpy(destination, word);
buffer[i * (length + 1) + length] = 10;
}
buffer[count * (length + 1)] = '\0';
printf("%s", buffer);
free(buffer);
}
1. 프로그램에 count inpu 으로 –1 이 주어지면, calloc 으로 얼마나 많은 메모리가 할당 되어야 하죠??
2. 위 코드에서 오류가 4개 있다고 하는데, 뭐뭐 있나요??