0

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;
Stc5097
  • 291
  • 1
  • 11
  • 25
  • 1
    Note: [You should never cast the result of malloc in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Paul R Mar 09 '14 at 20:26

3 Answers3

5

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);
Floris
  • 45,857
  • 6
  • 70
  • 122
1

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);
ajay
  • 9,402
  • 8
  • 44
  • 71
0

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 *.

Rob Jones
  • 4,925
  • 3
  • 32
  • 39
  • Please don't cast the result of `malloc`. See link by Paul R in comments above. And usually it is a good idea to write `pointer = malloc(num_elements * sizeof *pointer);` this makes sure that if you change your mind about the type of `pointer`, your `malloc` still works. Good defensive coding technique. – Floris Mar 09 '14 at 20:28
  • @Floris Yeah, I didn't want to completely rewrite the OPs code. I mean really, there's a ton wrong with it so why nitpick. He didn't bother to check the return value on malloc either, and therefore I didn't bother to include that in my response. – Rob Jones Mar 09 '14 at 20:34
  • @Floris Then edit your comment instead of posting another comment. – Rob Jones Mar 09 '14 at 20:37