Commit f7e04d15 by 정용석

Null 포인터 입력처리

parent 6537d9d8
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
unsigned int my_strlen(char *s){ unsigned int my_strlen(const char *s){
unsigned int i = 0; unsigned int i = 0;
while(s[i] != '\0'){ while(s[i] != '\0'){
i++; i++;
...@@ -10,17 +10,22 @@ unsigned int my_strlen(char *s){ ...@@ -10,17 +10,22 @@ unsigned int my_strlen(char *s){
return i; return i;
} }
void my_strcpy(char *s1, char *s2) { char *my_strcpy(char *dest, const char *src){
if(my_strlen(s1) <= my_strlen(s2)){ if(dest == NULL || src == NULL){
return NULL;
}
if(sizeof(dest) < my_strlen(src)){
printf("Error : 첫 번째 인자의 배열의 크기가 두 번째 인자의 크기보다 작기 때문에 정상적인 복사가 되지않습니다.\n"); printf("Error : 첫 번째 인자의 배열의 크기가 두 번째 인자의 크기보다 작기 때문에 정상적인 복사가 되지않습니다.\n");
return; return NULL;
} }
int i = 0; int i = 0;
while(s2[i] != '\0'){
s1[i] = s2[i]; while(src[i] != '\0'){
dest[i] = src[i];
i++; i++;
} }
s1[i] = '\0'; dest[i] = '\0';
return dest;
} }
int main() { int main() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment