简体   繁体   中英

Assigning a Pointer the value of another pointer

I am trying to assign the value of fileOrDir to the value of copyFileOrDir. I want copyFileOrDir to be equal to the value of fileOrDir, not point to the same address. I thought it would be copyFileOrDir = *filrOrDir but I get errors. below is my code: (fileOrDir gets its value from a command line argument)

char *fileOrDir = (char *)malloc(25*sizeof(char));
char *copyFileOrDir = (char *)malloc(25*sizeof(char));
copyFileOrDir = *fileOrDir;

Pointers point to a block of memory. If you set one pointer equal to another, you end up pointing to the same block of memory. If you actually assigned two different blocks, you should never want to then set one pointer to the other - you will be unable to free the memory.

Most likely you intend to do a memcpy which allows you to copy the contents of one memory block to another:

memcpy(void* destination, const void* source, size_t numberofbytes);

I am trying to assign the value of fileOrDir to the value of copyFileOrDir.

You don't assign the value of a variable to the value of another variable. That's wrong to say. You assign a value to a variable. Think of a variable as a memory location. Also don't cast the result of malloc . That's not useful and can lead to bugs. Now let's come to your code snippet.

// don't cast the result of malloc

char *fileOrDir = malloc(25 * sizeof(char)); 
char *copyFileOrDir = malloc(25 * sizeof(char));

The following statement

copyFileOrDir = *fileOrDir;

tries to assign the object pointed to by fileOrDir , which is of type char , to copyFileOrDir , which is of type char * - a different type. This is an error. Also, by assigning copyFileOrDir , you lose the handle on the memory allocated by malloc causing memory leak. If you want to copy the buffer pointed to by fileOrDir to the buffer pointed to by copyFileOrDir , you should use memcpy .

memcpy(copyFileOrDir, fileOrDir, 25);

I think you're confused about pointers and values. You said you want the "value of copyFileOrDir to be equal to the value of fileOrDir." But the value of fileOrDir is just a pointer to a block of dynamically allocated memory that happens to be 25 bytes (assuming sizeof(char) is one byte) in size.

What you really want is for copyFileOrDir to point to a block of memory that is an exact copy of the memory pointed to by the value of fileOrDir. You can do this with memcpy .

char *copyFileOrDir = malloc(25*sizeof(char)); memcpy(copyFileOrDir, fileOrDir, 25*sizeof(char));

Also, I should point out that copyFileOrDir = *fileOrDir; makes no sense. In that case you are dereferencing fileOrDir (ie getting the value at the address pointed to by fileOrDir , which is a char ) and assigning it to copyOfFileOrDir which is a char * . In other words, you are assigning a char to a char * .

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