1

How do I (m)allocate string in another function? I've tried to do it by 2 ways (down in code JUST EXAMPLE - I wouldn't do it with argv).

void someFunction(char* string, char** argv) {
    string = (char*)malloc(strlen(argv[2]) + 1);
    strcpy(string,argv[2]);
    // string[strlen(argv[2]) + 1] = '\0' //should I do this now?
    printf("%s",string); //outputs string (works fine)
}

int main(int argc, char** argv) {
    char *string; 
    char *string2;

    someFunction(string,argv);
    printf("%s",string); //outputs (null)
    free(string);

    someFunction(&*string2,argv);
    printf("%s",string2); //outputs (null)
    free(string2);
}

I want to use "string" in main, but mallocate it in different function.

Thank you!

Lucfia
  • 621
  • 1
  • 7
  • 14
  • Exactly what is your question, and/or what is the problem? – TonyB Apr 05 '16 at 22:09
  • @TonyB I want to use "string" in main, but mallocate it in different function. Is it possible? And how to send mallocated string as pointer? If I will use someFunction(string,argv), I won't be able to free that memory. – Lucfia Apr 05 '16 at 22:10
  • 1
    Without running it, what do you think `void f(int i) {i = 7;} int main(void) {int x = 5; f(x); printf("%i\n", x); return 0;}` prints? – user253751 Apr 05 '16 at 22:15
  • @immibis It prints 5 :) – Lucfia Apr 05 '16 at 22:28

2 Answers2

5

Here are two examples:

#include <stdio.h>
#include <string.h>
#include <malloc.h>

void alloc1 (char **new_string, char *text) {
  *new_string = malloc(strlen(text)+1);
  strcpy (*new_string, text);
}

char * alloc2(char *text) {

  char *new_string = malloc(strlen(text)+1);
  strcpy (new_string, text);
  return new_string;
}

int
main (int argc, char *argv[]) {
  char *s;

  /* Make sure we entered a command line argument */
  if (argc < 2) {
    printf ("USAGE: myprog SOMETHING\n");
    return 1;
  }

  alloc1(&s, argv[1]);
  printf ("alloc1: s=%s\n", s);
  free (s);

  s = alloc2 (argv[1]);
  printf ("alloc2: s=%s\n", s);
  free (s);

  return 0;
}

The first example passes a pointer to a pointer: your main allocates the pointer; the function allocates the space to the pointer; the pointer itself is passed by reference.

The second example is similar, except the pointer is returned by the function instead of altering the reference.

The first is "cleaner" if you're only initializing one pointer; the second is useful if you want to return multiple different values in the same function.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
2

You want this:

void someFunction(char** string, char** argv) {
    *string = (char*)malloc(strlen(argv[2]) + 1);
    strcpy(*string, argv[2]);
    // string[strlen(argv[2]) + 1] = '\0' //should I do this now?
    printf("%s",*string); //outputs string (works fine)
}

int main(int argc, char** argv) {
    char *string; 
    char *string2;

    someFunction(&string,argv);
    printf("%s",string);
    free(string);
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • Well, it fails to free the string for me when I use `strcpy(*string,'txt')` but that's not a part of my question. – Lucfia Apr 05 '16 at 22:21
  • @Lucfia What makes you think that `free` fails here? – Jabberwocky Apr 05 '16 at 22:22
  • sorry, I mean `strcat(*string,'txt')` – Lucfia Apr 05 '16 at 22:27
  • I believe it's more natural if you return the allocated string from that function. – Jongware Apr 05 '16 at 22:29
  • 1
    I don't see any `strcat` in your question. And it's `strcat(*string,"txt")` and not `strcat(*string,'txt')`. Maybe you should ask another question. – Jabberwocky Apr 05 '16 at 22:29
  • `strcat` may cause problems if the buffer is not long enough. E.g. you allocate 6 bytes for the string "Hello". And then you do `strcat`on that buffer, which will write beyond the end of the buffer causing undefined behaviour. – Jabberwocky Apr 05 '16 at 22:32
  • @MichaelWalz THANK YOU! Atleast you solved my st**id mistake in all of my programs :)). – Lucfia Apr 05 '16 at 22:38