0

in my main function I initialize a char pointer. This pointer I overgive a function setMemory(char *ptr) as a parameter where some memory shall be allocated. Additionally some data shall be stored in this function. Back in main function I try to read out the data to which the pointer shows, but not the correct data were outputted. Why?

int main(int argc, char *argv){
   char *ptr;
   setMemory(ptr);
   printf("String: %s", ptr); //Should print c
   return 0;
}

void setMemory(char *ptr){
   ptr = (char*)malloc(sizeof(char)*10);
   *(ptr) = 'c';
}
florian2840
  • 49
  • 3
  • 10
  • Possible duplicate of ["Initializing" the pointer in the separate function in C](http://stackoverflow.com/questions/2486235/initializing-the-pointer-in-the-separate-function-in-c) – Bo Persson Oct 19 '15 at 17:08

3 Answers3

1

Based on ameyCU answer

char* setMemory(int size){
    char *ptr = calloc(size, sizeof(char)); //Used calloc instead of malloc + memeset to \0
    *ptr = 'c';
    return ptr;
}

And called

char *ptr = setMemory(10);
Mr. E
  • 2,070
  • 11
  • 23
0

Right now you're only passing a copy of the pointer, and modifying the local copy in setMemory.

If you want to modify the original pointer passed to setMemory, you need to pass a pointer to it:

void setMemory(char **ptr) {
   *ptr = malloc(sizeof(char) * 10);
   (*ptr)[0] = 'c';
   (*ptr)[1] = '\0';
}

Note the null character, which is needed to terminate each C-style string.

and call it with

setMemory(&ptr);

Also, don't cast the result of malloc, it's redundant.

Also your main() signature is wrong, char *argv should be char **argv (or preferably const char **argv, since modifying **argv is undefined behavior anyway).

Community
  • 1
  • 1
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • I tried this version in Codeblocks as a terminal application and it was working. But when I port it to my C projekt for the MSP430 in Code Composer Studio I always get the error: argument of type "char ***" is incompatible with parameter of the type "char **" when I am calling setMemory(&ptr) – florian2840 Oct 21 '15 at 11:03
  • @florian2840 `ptr` should be of type `char*`, not `char**`, as in your original snippet. Or if you changed `ptr` to be `char**` then you can probably just do `setMemory(ptr)`. – Emil Laine Oct 21 '15 at 11:07
  • The question is why it is working in the console application? In my main function I can print the content of ptr like this printf("String: %s, ptr)? Badly this printing is not possible. There is no content printed except String::( – florian2840 Oct 21 '15 at 11:19
  • You forgot about the null terminator, check my updated answer. – Emil Laine Oct 21 '15 at 11:23
  • no content yet, also with the null termination. My console application is identical to the code composer version and the first one is working, the second not. – florian2840 Oct 21 '15 at 11:28
  • If it's identical to your original code with the code from my answer substituted then it should work. If not then you've done something wrong, but if you want help with that you should post a new question about specifically that. – Emil Laine Oct 21 '15 at 11:37
  • probably it's a stack size problem, so I solved the issue – florian2840 Oct 21 '15 at 12:56
0

Another way is to return the char * from you function to main -

char *setMemory(char *ptr){
    ptr = malloc(sizeof(char)*10);          // allocate memory
    memset(ptr,'\0',sizeof(char)*10);            // initialize ptr
    *(ptr) = 'c';
    return ptr;                             // return ptr 
 }

And in main call it like this -

char *ptr;
ptr=setMemory(ptr);

Note - Also correct type of argv as mentioned by in answer by Zenith. And also free the allocated memory.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • What's the use of the `ptr` parameter when you overwrite it right away? – Emil Laine Oct 21 '15 at 11:25
  • `ptr = …` overwrites the old value of `ptr`, before you read anything from it. So the argument is redundant, the function works the same regardless of what is passed to it. – Emil Laine Oct 21 '15 at 11:34
  • Also `sizeof ptr` == `sizeof(char*)` == size of pointer != `sizeof(char)*10`. – Emil Laine Oct 21 '15 at 11:35
  • @zenith Ohh , now I see it . Clearly missed it , Thanks for pointing out :-) – ameyCU Oct 21 '15 at 11:35
  • @zenith `sizeof` part was a clear mistake . But over-writing here should not be a problem because it does not point to anything before I think . – ameyCU Oct 21 '15 at 11:38