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;

Answers


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 (i.e. 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 *.


Need Your Help

Find next sibling at the same level if exists

python html beautifulsoup bs4 nextsibling

I'm curious whether is it possible to get next sibling only when is there some in the same level.

Creating an Object and initialize it with passing parameters in Java

java object parameters

I am trying to create a new object and initialize it with the parameters which are passed by the following command :