Saturday, February 16, 2019

Value of type error in C++ function always NULL

i have to do my function always.I can not using standart library.




My_cpy , my_len and my_strdup function in here. Please check it for me. I think it is easy but i have a problem about this function. I showed the error at the end of the page. I think it is clear. In addition this is C++



Thanks a lot.



Codes:



void my_cpy(char* dest, const char* src) {

int i = 0;
while (src[i] != '\0') {

dest[i] = src[i];
i++;
}
dest[i] = '\0';
}

int my_len(const char* p) {

int c = 0;
while (*p != '\0')

{
c++;
*p++;
}
return c;
}

char *my_strdup(const char *s) {
char* d = malloc(my_len(s) + 1); // Space for length + null
if (d == NULL) return NULL; //No memory

my_cpy(d, s); // Copy the characters
return d; // Return the new string
}


I have error on this functions. How can i solve this problem?




Error (active) a value of type "void *" cannot be used to initialize
an entity of type "char *"




`Error    C2440   'initializing': cannot convert from 'void *' to 'char *'`



I wrote it:



char* d = (char*) malloc(my_len(s) + 1)



but now problem on p . Always NULL.

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...