Make a copy of char *

I have a function that takes char * as one of its parameters. I need to manipulate it, but leave the original char * intact. Essentially, I want to create a working copy of this char *. It seems to be easy, but I'm really struggling.

My first (naive) attempt was to create another char * and set it equal to the original:

char* linkCopy = link; 

This does not work, of course, because all I did was get them to point to the same place.

Should I use strncpy to accomplish this?

I tried the following, but it fails:

 char linkCopy[sizeof(link)] = strncpy(linkCopy, link, sizeof(link)); 

Did I miss something obvious ...?

EDIT: My apologies, I tried to simplify the examples, but I left some of the longer variable names in the second example. Fixed.

+6
c string
source share
7 answers

sizeof will give you the size of the pointer. Which is often 4 or 8 depending on your processor / compiler, but not the size of the specified string. You can use strlen and strcpy:

 // +1 because of '\0' at the end char * copy = malloc(strlen(original) + 1); strcpy(copy, original); ... free(copy); // at the end, free it again. 

I saw some answers suggest using strdup , but that strdup function, not part C.

+23
source share

You can take a look at the strdup ( man strdup ) function:

 char *linkCopy = strdup(link); /* Do some work here */ free(linkCopy); 

Change And since you need it to be standard C, do as others have pointed out:

 char *linkCopy = malloc(strlen(link) + 1); /* Note that strncpy is unnecessary here since you know both the size * of the source and destination buffers */ strcpy(linkCopy, link); /* Do some work */ free(linkCopy); 

Since strdup () is not ANSI / ISO C compliant, if it is not available in your compiler runtime, use this:

 /* ** Portable, public domain strdup() originally by Bob Stout */ #include <stdlib.h> #include <string.h> char* strdup(const char* str) { char* newstr = (char*) malloc( strlen( str) + 1); if (newstr) { strcpy( newstr, str); } return newstr; } 
+15
source share

Use strdup or strndup if you know the size (more secure).

how

 char* new_char = strdup(original); ... manipulate it ... free(new_char) 

ps .: not standard C

+1
source share

Some answers, including those accepted, are a bit irrelevant. You are not strcpy the string you just strlen'd. strcpy should not be used at all in modern programs.

The right thing is memcpy.

EDIT: memcpy is likely to be faster in any architecture, strcpy can only work better for very short lines and should be avoided for security reasons, even if they are not relevant in this case.

+1
source share

You're on the right track, you need to use strcpy / strncpy to create copies of strings. Simply assigning them simply makes an β€œalias” of it, another name points to the same thing.

Your main problem in your second attempt is that you cannot assign an array this way. The second problem is that you seem to have come up with new names in the function call that I cannot determine where they came from.

What would you like:

 char linkCopy[sizeof(link)]; strncpy(linkCopy, chLastLink, sizeof(link)); 

but be careful, sizeof doesn't always work the way you want it to be on lines. Use strlen or use strdup.

0
source share

Like sean.bright, strdup () is the easiest way to handle a copy. But strdup (), although widely available, is not std C. This method also saves the copied string on the heap.

 char *linkCopy = strdup(link); /* Do some work here */ free(linkCopy); 

If you intend to use the stack-highlighted string and strncpy (), you will need some changes. You wrote:

 char linkCopy[sizeof(link)] 

This creates a char (aka string) array on the stack, a pointer size (possibly 4 bytes). The third parameter for strncpy () has the same problem. You probably want to write:

 char linkCopy[strlen(link)+1]; strncpy(linkCopy,link,strlen(link)+1); 
0
source share

You are not saying whether you can use C ++ instead of C, but if you can use C ++ and STL, this is even simpler:

 std::string newString( original ); 

Use newString , since you would use the above copy of the C style, its semantics are identical. You do not need free() , it is a stack object and will be deleted automatically.

0
source share

All Articles