Quote:
Why, in this example, I can't print the ptr value?
|
Ok well call1 is a really bad function and should cause a segmentation fault.
call2: you create a pointer in teste and pass to call2, the value of the pointer could be anything. Inside the function you then allocate memory and copy a string into it, yet when the function returns it will be just junk in the pointer. What is needed is to pass a pointer to pointer like so:
Code:
void teste()
{
char* ptr;
call2(&ptr);
printf("%s\n", ptr);
}
void call2(char** ptr)
{
*ptr = malloc(strlen("HELLO"));
strcpy(ptr,"HELLO");
}
Quote:
2 - Second questio. If I do a malloc inside a functin, when I return it, the value is deallocated?
|
No see above, but you do have a memory leak.
Quote:
If when I call a function that would put a value in the parameter char*, and I don't know what value it will be. How should I do it?
|
I don't think I fully understand this question, you have already shown you know how to get a string length.
Quote:
I the function accepts a char* var[] and a char** var2 parameters, how should I do it?
|
You will have seen this before many times in a main function.
int main ( int argc, char* argv[] )
or
int main ( int argc, char** argv )