Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a copy of a char*

Tags:

c

string

I have a function that accepts a 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 like this should be easy, but I am really struggling.

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

char* linkCopy = link;

This doesn't work, of course, because all I did was cause them to point to the same place.

Should I use strncpy to accomplish this?

I have tried the following, but it causes a crash:

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

Am I missing something obvious...?

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

like image 504
Tim Avatar asked Jan 26 '09 22:01

Tim


People also ask

What does char * mean in C?

In C, char* means a pointer to a character. Strings are an array of characters eliminated by the null character in C.

Can you strcpy a char?

You can use strcpy with both pointers and arrays and it will work since data from both can be overwritten. "char[] is not dynamic", not entirely true. You can use VLA, or the array is part of struct which is dynamically allocated.

How do you copy characters to chars?

Copying one string to another - strcpystrcpy can be used to copy one string to another. Remember that C strings are character arrays. You must pass character array, or pointer to character array to this function where string will be copied. The destination character array is the first parameter to strcpy .

Is char * Same as string?

The difference between a string and a char* is that the char* is just a pointer to the sequence. This approach of manipulating strings is based on the C programming language and is the native way in which strings are encoded in C++.


2 Answers

The 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 string pointed to. 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've seen some answers propose use of strdup, but that's a posix function, and not part of C.

like image 73
Johannes Schaub - litb Avatar answered Oct 06 '22 08:10

Johannes Schaub - litb


You might want to take a look at the strdup (man strdup) function:

char *linkCopy = strdup(link);

/* Do some work here */

free(linkCopy);

Edit: 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 in ANSI/ISO standard C, if it's not available in your compiler's runtime, go ahead and 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;
}
like image 42
Sean Bright Avatar answered Oct 06 '22 07:10

Sean Bright