1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char *fct_strdup(char *src)
{
char *dest;
int i;
int LEN;
i = 0;
if (src == NULL)
return (NULL);
while (src[LEN] != '\0')
{
LEN++; /* ma fct strlen pour LEN */
}
LEN = LEN + 1;
dest = (char*)malloc(sizeof(*dest) * LEN); /* appel malloc */
while (src[i] != '\0')
{
dest[i] = src[i];
i++; /* ma fct strcpy */
}
dest[i] = '\0';
free(dest);
return(dest);
}
int main()
{
char *src;
src = ""; /* mon problème ... */
printf("string before strdup : %s\n\n", *src);
*fct_strdup(*src);
printf("string after strdup : %s\n", *src);
return (0);
} |