【问题标题】:Passing a pointer to a function and change the address it points to [duplicate]将指针传递给函数并更改它指向的地址[重复]
【发布时间】:2020-01-25 06:28:40
【问题描述】:

对于一段嵌入式代码 (avr-gcc),我正在尝试减少堆栈内存使用量。所以我想做的是创建一个指针,将它传递给一个函数,然后在函数中,将指针指向的地址更改为堆分配变量的地址。这样,main() 内部就不会为 testPointer 分配堆栈内存。

我正在尝试使用以下代码

#include <stdio.h>
char hello[18] = "Hello cruel world";
char* myfunc2() {
        return hello;
}
void myfunc(char *mypointer) {
        mypointer = myfunc2();
}
int main(){
        char *testPointer;
        printf("hello: %p\n", &hello);
        printf("test: %p\n", &testPointer);
        myfunc(testPointer);
        printf("test: %p\n", &testPointer);
        printf("test value: %s\n", testPointer);
        return 0;
}

但 testPointer 地址不会被重新分配。当然,在现实世界中,myfunc2 的用例不会那么简单,但它返回一个指向堆分配字符数组的指针。

输出:

hello: 0x404030
test: 0x7ffe48724d38
test: 0x7ffe48724d38
test value: (null)

【问题讨论】:

  • 你需要研究一下静态存储和堆的区别。您的 AVR 程序中应该没有堆,并且此源中没有堆分配。此外,做你试图减少堆栈使用的方法是无稽之谈。相反,您应该专注于实际杀死 MCU 上所有内存的原因,首先将 stdio.h 扔到它所属的垃圾中。

标签: c


【解决方案1】:

您传递一个指向您要写入的位置的指针。比较:

#include <stdio.h>
char hello[18] = "Hello cruel world";
char* myfunc2() {
        return hello;
}
void myfunc(char **mypointer) {
        *mypointer = myfunc2();
}
int main(){
        char *testPointer;
        printf("hello: %p\n", &hello);
        printf("test: %p\n", &testPointer);
        myfunc(&testPointer);
        printf("test: %p\n", &testPointer);
        printf("test value: %s\n", testPointer);
        return 0;
}

【讨论】:

    【解决方案2】:

    你需要传递双指针。

    一般来说,如果你想改变一些其他函数的值,你需要将该变量的地址传递给其他函数。这样一来,您正在尝试更改存储在变量中的地址,因此您需要传递指针变量的地址。

    void myfunc(char **mypointer) { //Get address of a pointer variable
            if(NULL != mypointer ) //Check if variable is NULL
                *mypointer = myfunc2(); //Otherwise, update the pointer value
    }
    

    用指针变量的地址调用函数:

    myfunc(&testPointer);
    

    【讨论】:

      【解决方案3】:

      我真的不明白你想做什么,但无论如何,关于你的代码有两个问题:

      1) 你按值传递函数参数。它应该通过引用传递

      2)您通过&amp;hello打印变量的地址:它表示存储指针的地址而不是它指向的地址(即变量hello的值)。

      你应该这样做:

      #include <stdio.h>
      char hello[18] = "Hello cruel world";
      char* myfunc2() {
              return hello;
      }
      void myfunc(char **mypointer) {
              *mypointer = myfunc2();
      }
      int main(){
              char *testPointer;
              printf("hello: %p\n", hello);
              printf("test: %p\n", testPointer);
              myfunc(&testPointer);
              printf("test: %p\n", testPointer);
              printf("test value: %s\n", testPointer);
              return 0;
      }
      

      输出是:

      hello: 0x601040                                                                                                             
      test: (nil)                                                                                                                 
      test: 0x601040                                                                                                              
      test value: Hello cruel world
      

      请注意,第一次打印指针test的值时,它是空指针。它不是存储变量test的地址。

      但是,我看不出这段代码应该做什么......

      【讨论】:

      • 我猜这只是一些用来玩指针的测试代码!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-26
      • 2014-03-01
      • 2020-01-09
      • 2018-04-05
      • 2013-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多