简体   繁体   English

为Pointer分配另一个指针的值

[英]Assigning a Pointer the value of another pointer

I am trying to assign the value of fileOrDir to the value of copyFileOrDir. 我正在尝试将fileOrDir的值分配给copyFileOrDir的值。 I want copyFileOrDir to be equal to the value of fileOrDir, not point to the same address. 我希望copyFileOrDir等于fileOrDir的值,而不是指向相同的地址。 I thought it would be copyFileOrDir = *filrOrDir but I get errors. 我以为会是copyFileOrDir = * filrOrDir,但出现错误。 below is my code: (fileOrDir gets its value from a command line argument) 下面是我的代码:(fileOrDir从命令行参数获取其值)

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 ,它允许您将一个内存块的内容复制到另一个内存块:

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

I am trying to assign the value of fileOrDir to the value of copyFileOrDir. 我正在尝试将fileOrDir的值分配给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 . 也不要转换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. 尝试将fileOrDir指向的对象(其类型为char )分配给copyFileOrDir ,该对象的类型为char * -另一种类型。 This is an error. 这是一个错误。 Also, by assigning copyFileOrDir , you lose the handle on the memory allocated by malloc causing memory leak. 另外,通过分配copyFileOrDir ,您将丢失由malloc分配的malloc上的句柄,从而导致内存泄漏。 If you want to copy the buffer pointed to by fileOrDir to the buffer pointed to by copyFileOrDir , you should use memcpy . 如果要复制缓冲区指向fileOrDir到缓冲区指向copyFileOrDir ,你应该使用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." 您说过想要“ copyFileOrDir的值等于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. 但是fileOrDir的值只是指向动态分配的内存块的指针,该块恰好是25个字节(假设sizeof(char)是一个字节)。

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. 您真正想要的是copyFileOrDir指向一个内存块,该内存块是fileOrDir值所指向的内存的精确副本。 You can do this with memcpy . 您可以使用memcpy进行此操作。

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

Also, I should point out that copyFileOrDir = *fileOrDir; 另外,我应该指出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 * . 在这种情况下,您将取消引用fileOrDir (即,在由fileOrDir指向的地址处获取值,即char ),并将其分配给具有char * copyOfFileOrDir In other words, you are assigning a char to a char * . 换句话说,您正在将char分配给char *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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