简体   繁体   中英

Working with Pointers and Strcpy in C

I'm fairly new to the concept of pointers in C. Let's say I have two variables:

char *arch_file_name;
char *tmp_arch_file_name;

Now, I want to copy the value of arch_file_name to tmp_arch_file_name and add the word "tmp" to the end of it. I'm looking at them as strings, so I have:

strcpy(&tmp_arch_file_name, &arch_file_name);
strcat(tmp_arch_file_name, "tmp");

However, when strcat() is called, both of the variables change and are the same. I want one of them to change and the other to stay intact . I have to use pointers because I use the names later for the fopen(), rename() and delete() functions. How can I achieve this?

What you want is:

strcpy(tmp_arch_file_name, arch_file_name);
strcat(tmp_arch_file_name, "tmp");

You are just copying the pointers (and other random bits until you hit a 0 byte) in the original code, that's why they end up the same.

As shinkou correctly notes, make sure tmp_arch_file_name points to a buffer of sufficient size (it's not clear if you're doing this in your code). Simplest way is to do something like:

char buffer[256];
char* tmp_arch_file_name = buffer;

First, you need to make sure those pointers actually point to valid memory. As they are, they're either NULL pointers or arbitrary values, neither of which will work very well:

char *arch_file_name = "somestring";
char tmp_arch_file_name[100]; // or malloc

Then you cpy and cat , but with the pointers, not pointers-to-the-pointers that you currently have:

strcpy (tmp_arch_file_name, arch_file_name);  // i.e., no "&" chars
strcat (tmp_arch_file_name, "tmp");

Note that there is no bounds checking going on in this code - the sample doesn't need it since it's clear that all the strings will fit in the allocated buffers.

However, unless you totally control the data, a more robust solution would check sizes before blindly copying or appending. Since it's not directly related to the question, I won't add it in here, but it's something to be aware of.

Before you use pointers, you need to allocate memory. Assuming that arch_file_name is assigned a value already, you should calculate the length of the result string, allocate memory, do strcpy , and then strcat , like this:

char *arch_file_name = "/temp/my.arch";
// Add lengths of the two strings together; add one for the \0 terminator:
char * tmp_arch_file_name = malloc((strlen(arch_file_name)+strlen("tmp")+1)*sizeof(char));
strcpy(tmp_arch_file_name, arch_file_name);
//    ^ this   and   this ^ are pointers already; no ampersands!
strcat(tmp_arch_file_name, "tmp");

// use tmp_arch_file_name, and then...
free(tmp_arch_file_name);

The & operator is the address-of operator, that is it returns the address of a variable. However using it on a pointer returns the address of where the pointer is stored, not what it points to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM