【问题标题】:passing address of variable to a C function将变量的地址传递给 C 函数
【发布时间】:2013-08-09 19:52:14
【问题描述】:

我对 C 语言比较陌生。我正在尝试将变量的地址传递给函数,然后让该函数为传递的该变量地址分配一个 char 指针。编译器没有报错,但代码也不能正常工作。

typedef enum {
    VAL_1,
    VAL_2
} member_type;

char *a1="Test 123";

int func (member_type x, char *temp) {

    switch(x) {
        case VAL_1:
             temp = a1;
             return 1;
        case VAL_2:
             return 2;
    }
    return 0;
}

int main(){

    member_type b;
    static char *d1;
    b = VAL_1;
    printf("%p\n",&d1);

    func(b, &d1);

    printf("Val_1:%s\n",d1);

    return 0;
}

执行时出现以下错误:

-bash-3.00$ ./a.out
 0x500950
 Name:(null)

谁能帮我解决一下?

【问题讨论】:

    标签: c string pointers char memory-address


    【解决方案1】:

    我觉得很奇怪你的编译器没有抱怨。我怀疑你在编译时没有警告。您应该始终使用启用的-Wall 选项进行编译(假设您使用的是 GCC 或 clang)。

    您做错的是,虽然您将char * 指针的地址传递给您的函数,但您只修改了该指针的本地副本(函数参数在C 中按值传递),这没有任何效果函数之外。您应该做的是将函数参数声明为指向指针的指针,并通过取消引用其地址来修改原始指针:

    void func(const char **p) // notice the pointer-to-pointer...
    {
        *p = "bar"; // and the dereference (*) operator
    }
    
    const char *p = "foo";
    printf("Before: %s\n", p);
    func(&p);
    printf("After: %s\n", p);
    

    打印出来:

    Before: foo
    Afte: bar
    

    【讨论】:

      【解决方案2】:

      您需要双重取消引用:

      typedef enum {
          VAL_1,
          VAL_2
      } member_type;
      
      char *a1="Test 123";
      
       int func (member_type x, char **temp) {
      
                switch(x) {
                  case VAL_1:
                      temp = &a1;
                      return 1;
      
                  case VAL_2:
                      return 2;
       }
       return 0;
      }
      
      int main(){
      
        member_type b;
        static char *d1;
        b = USERNAME;
        printf("%p\n",&d1);
      
        func(USERNAME, &d1);
      
        printf("Val_1:%s\n",d1);
      
        return 0;
      }
      

      【讨论】:

        【解决方案3】:

        复制了您的代码并在 func 中仅进行了两项更改:1) char **temp 和 *temp = a1。回想一下,a1 和 *temp 一样是一个指针。

        typedef enum {
            VAL_1,
            VAL_2
        } member_type;
        
        char *a1 = "Test 123";
        
         int func (member_type x, char **temp) {
        
                  switch(x) {
                    case VAL_1:
                        *temp = a1;   // need to dereference the double ptr
                        return 1;
        
                    case VAL_2:
                        return 2;
         }
         return 0;
        }
        
        int main(){
        
          member_type  b;
          static char   *d1;
        
          b = VAL_1;
          printf("%p\n", &d1);     // point to the pointer d1
        
          func(b, &d1);      // point to the pointer d1
        
          printf("Val_1:%s\n",d1);
        
          return 0;
        }
        

        在 Eclipse/Microsoft C 编译器上运行此代码,并打印:

        004054A0
        Val_1:Test 123
        

        【讨论】:

          猜你喜欢
          • 2016-04-28
          • 1970-01-01
          • 1970-01-01
          • 2020-01-09
          • 1970-01-01
          • 2014-12-06
          • 1970-01-01
          • 1970-01-01
          • 2014-04-03
          相关资源
          最近更新 更多