简体   繁体   English

在C中保存一个指针地址

[英]Save a pointer address in C

I want to save a certain address of a pointer, and use it later. 我想保存一个指针的特定地址,并在以后使用。

This is the struct which holds the original pointer: 这是保存原始指针的结构:

typedef struct CSV
{
    char *RD;
    ...
}CSV;

This is the called function: 这就是所谓的函数:

static status_t write_to_buffer(CSV *CSVUtil,...)
{

    // The way i was planning to save address:
    char* temp =  &CSVUtil->RD;

...
    // pointer location ++
    CSVUtil->RD++;
...
    // The way i wanted to restore it:
    &CSVUtil->RD = temp;
}

First, am i doing the address restoring as needed? 首先,我是否在根据需要恢复地址?

I get this error message: expression must be a modifiable lvalue 我收到此错误消息: expression must be a modifiable lvalue

So i'm guessing i'm not, But what can i do to fix this? 所以我想我不是,但是我该怎么做才能解决此问题?

EDIT: 编辑:

Just to be clear, what i want to do is to copy the address to a certain pointer, change the used address (increment it), and than set back the copied address to the used address. 为了清楚起见,我要执行的操作是将地址复制到某个指针,更改使用的地址(递增),然后将复制的地址重新设置为使用的地址。

Cheers. 干杯。

This assignment is incorrect: 此分配不正确:

char* temp =  &CSVUtil->RD;

The expression &CSVUtil->RD returns a pointer to a character pointer (ie char** ), but you are assigning it to a character pointer char* , so one level of indirection is missing. 表达式&CSVUtil->RD返回一个指向字符指针的指针(即char** ),但是您将其分配给字符指针char* ,因此缺少一级间接寻址。 There should be a compiler warning in the output telling you about this problem. 输出中应该有一个编译器警告,告诉您有关此问题的信息。

The reason the assignment back &CSVUtil->RD = temp does not work is that the result of the "take address & " operator is not assignable. 分配&CSVUtil->RD = temp的分配不起作用的原因是“接受地址& ”运算符的结果不可分配。 You can obtain an address, but you cannot change it by "assigning" it a new address. 您可以获得地址,但是不能通过“分配”新地址来更改它。

It looks like you need to save and restore the pointer itself, not the location of that pointer. 看起来您需要保存和还原指针本身,而不是该指针的位置。 Therefore, you can fix the code by removing the ampersands: 因此,可以通过删除与号来修复代码:

char* temp =  CSVUtil->RD;
...
CSVUtil->RD = temp;

According to your problem 根据你的问题

expression must be a modifiable lvalue 表达式必须是可修改的左值

That means that your are trying to assign a value to a const or an Un-Changeable type. 这意味着您正在尝试为const或Un-Changeable类型分配值。

Now lets take a look at the assignments you have: 现在,让我们看一下您的作业:

  1. char* temp = &CSVUtil->RD; char * temp =&CSVUtil-> RD;
  2. CSVUtil->RD++; CSVUtil-> RD ++;
  3. &CSVUtil->RD = temp; &CSVUtil-> RD = temp;

A Pointer as you know, holds an address so if i have a: 如您所知,指针拥有一个地址,所以如果我有:

char *pointer char *指针

The pointer variable will hold the address where the actual value i want is, and to access that value i would either use *pointer = XXX or if it's a struct pointer->inside-value = XXX. 指针变量将保存我想要的实际值所在的地址,并且要访问该值,我将使用* pointer = XXX或结构指针-> inside-value = XXX。

What you are trying to do by adding the "&" in front of the pointer is to actually give back the location of "pointer" variable which was declared. 您试图通过在指针前面添加“&”来实际上返回已声明的“ pointer”变量的位置。

So - instead of getting the address of the value that RD is pointing to, you get the address of RD itself. 因此,您无需获取RD指向的值的地址,而是获取RD本身的地址。 ALSO and here is the problem - Addresses of variables cannot be changed this way but through malloc as it's being decided in compilation or runtime if it's dynamic (malloc) so when doing &Pointer = XXXX you are actually aren't changing the variable but trying to change it's address so to speak. 还存在问题-变量的地址不能以这种方式更改,而是通过malloc更改,因为它是动态的(malloc)在编译或运行时决定,因此在执行&Pointer = XXXX时,您实际上并没有更改变量,而是尝试可以这么说来改变它的地址。 BUT the "&" operator just returns the address (think of it like a function that returns an int that specifies address) so it cannot be changed. 但是“&”运算符只是返回地址(想想它就像返回指定地址的int的函数一样),因此它不能被更改。

I would change the assignments to: 我将分配更改为:

  1. char *temp = CSVUtil->RD; char * temp = CSVUtil-> RD;
  2. Is ok; 还可以
  3. CSVUtil->RD = temp; CSVUtil-> RD = temp;

That should work. 那应该工作。

Hope my explanation was good enough. 希望我的解释足够好。

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

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