0

I want to know the efficient way of assigning the string or copying the string using the pointer.

Using strcpy() is better than just assigning pointer to the pointer?

void local_var() {
    // method 1
    char *a = "simple test";
    char *b;
    b = a;
    printf("a:%s\n", b);    

    fflush(stdout);
    //method 2
    char *c = "test test";
    char *d;
    d=malloc(sizeof(char) * strlen(c));
    strcpy(d,c); 
    printf("a:%s\n", d);
    free(d);  //EDIT: I missed to add in typing the question.
}
  • 3
    It depends. What is your goal? What is the problem you want to solve? What is the use-case? – Some programmer dude Sep 01 '17 at 09:22
  • 4
    Oh and you have an off-by-one error in your code, and the `strcpy` call will write out of bounds of your allocated memory, leading to *undefined behavior*. You forget to allocate space for the string terminator. – Some programmer dude Sep 01 '17 at 09:23
  • 1
    1st method: no copy of string made. 2nd method: copy of string made. What you want to do depends on your use case. – Andre Kampling Sep 01 '17 at 09:23
  • 4
    You leak memory because you don't free what you allocate. Both techniques work; which is appropriate depends on what you're going to do with the copy. And you have a buffer overflow; you don't allocate enough space. – Jonathan Leffler Sep 01 '17 at 09:23
  • 3
    Lastly a small tip: When making a pointer to a literal string, use `const char *`. That's because literal strings are *read only*, they can not be modified (which might give a partial answer to your question). – Some programmer dude Sep 01 '17 at 09:24
  • There is nothing here to talk about. The literal string where in method one is used is there to be used but you can't modify it and in method two after you copy it, you can. – Michi Sep 01 '17 at 09:25
  • 1
    *"Using strcpy() is better than just assigning pointer to the pointer?"* - better for *what* ? Performance? Obviously assigning a pointer is going to be performant over invoking even a library intrinsic, much less invoking memory allocation, which is generally very expensive. The intended *use* of the "copy" ultimately tells which is "better", and your post seems to avoid stating any intended use, so there is no clear way to answer this question. These are two very different things. – WhozCraig Sep 01 '17 at 09:26
  • This is the perfect thing to make you understand where the difference is between a Pointer and an Array. It's kind the same thing. – Michi Sep 01 '17 at 09:28
  • right I will add +1 for the strlen() where it needs space for string termination. –  Sep 01 '17 at 09:31
  • thanks all for the answer, I just want to know the pros of using `strcpy()` and pointer. It's clear that using string literal the value can't be modified. –  Sep 01 '17 at 09:37
  • 1
    On performance: On my computer a pointer takes 8 bytes, so `b=a;` copies 8 bytes while `strcpy(d,c);` copies 10 bytes. Wanna guess how much longer that takes at 4 GHz? :-) – Bo Persson Sep 01 '17 at 11:07

1 Answers1

2

They don't do the same things.

Assigning a pointer, is just creating a new link to the same data. If you are modifying the data using b, it will affect as well the data behind a.

In other hands using strcpy/malloc create a new data independent to the first one.


Here is a little schema

enter image description here

Orelsanpls
  • 22,456
  • 6
  • 42
  • 69