【问题标题】:Accessing pointer member of the structure using structure pointer [closed]使用结构指针访问结构的指针成员
【发布时间】:2016-01-01 06:21:17
【问题描述】:

我已经在结构体内部定义了整数指针。我想使用结构体指针来使用该成员指针。我的代码如下所示:

#include<stdio.h>
#include<stdlib.h>

struct abc
{
        int *x;
};

int main()
{

        struct abc *p = (struct abc*)malloc(sizeof(struct abc));
        p->x = (int*)malloc(sizeof(int));
        p->x = 10;
        printf("The value is %d\n",p->x);
        free(p);
}

现在我得到了符合我期望的输出。但是我在编译时收到了警告消息。警告消息是:

temp.c:14:7: warning: assignment makes pointer from integer without a cast [enabled by default]
temp.c:15:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]

我也试过了,

*p->x = 10
 printf("The value is %d\n",*p->x);

但它不起作用。

如何解决这个警告?

【问题讨论】:

  • *p-&gt;x = 10; printf("The value is %d\n",*p-&gt;x);

标签: c pointers data-structures


【解决方案1】:

为了给指针指向的内存地址赋值,当指针用作左值时,你必须取消引用指针。例如:

*p->x = 10;

在分配内存时,不需要强制转换malloc的返回值。 malloc(和calloc 等)只返回一个没有类型(或类型为void)的内存地址。此外,当您使用'sizeof object' 时,您消除了指定类型时出错的风险(这在使用typedef 时变得更加明显,等等)。例如,您的分配应该是:

struct abc *p = malloc (sizeof *p);
p->x = malloc (sizeof *p->x);

最后,在您编写的动态分配内存的任何代码中,对于分配的任何内存块,您都有 2 个责任:(1)始终保留指向内存块起始地址的指针,(2)它可以是不再需要时释放。

您必须使用内存错误检查程序来确保您没有超出/超出分配的内存块,尝试读取或基于未初始化的值进行跳转,最后确认您已释放所有您分配的内存。这是你没有做的事情。如果您分配它,当它不再需要时free它。例如:

free (p->x);
free (p);

有许多微妙的方法可以滥用新的内存块。使用内存错误检查器可以让您识别任何问题并验证您分配的内存的正确使用,而不是通过segfault 发现问题存在。对于 Linux,valgrind 是内存错误检查器的正常选择。每个平台都有类似的内存检查器。它们都易于使用,只需通过它运行您的程序即可。例如:

$ valgrind ./bin/struct_simple
==21079== Memcheck, a memory error detector
==21079== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==21079== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==21079== Command: ./bin/struct_simple
==21079==

 The value is 10

==21079==
==21079== HEAP SUMMARY:
==21079==     in use at exit: 0 bytes in 0 blocks
==21079==   total heap usage: 2 allocs, 2 frees, 12 bytes allocated
==21079==
==21079== All heap blocks were freed -- no leaks are possible
==21079==
==21079== For counts of detected and suppressed errors, rerun with: -v
==21079== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)

您要确认All heap blocks were freed -- no leaks are possibleERROR SUMMARY: 0 errors from 0 contexts

祝你在新的一年编码好运。

【讨论】:

    【解决方案2】:

    唯一的问题是一个错字-

    *p->x= 10               /* <-- missing ;  */
    printf("The value is %d\n",*p->x);
    

    还有free(p-&gt;x) 在释放p 之前。

    Working example

    【讨论】:

      【解决方案3】:

      像这样改变两行:

      *p->x = 10;
      
      printf("The value is %d\n",*p->x);
      

      它会起作用的..

      【讨论】:

        【解决方案4】:
        "p->x" can hold only address of an integer as it is a pointer. So instead of value assign address to it.
        
        struct abc
        {
                int *x;
        };
        
        int main()
        {
                int i = 10;
                struct abc *p = (struct abc*)malloc(sizeof(struct abc));
                p->x = (int*)malloc(sizeof(int));
                p->x = &i;
                printf("The value is %d\n",*p->x);
                free(p);
        }
        

        【讨论】:

          【解决方案5】:

          试试这个:

          #include<stdio.h>
          #include<stdlib.h>
          
          struct abc
          {
                  int *x;
          };
          
          int main()
          {
          
                  struct abc *p = (struct abc*)malloc(sizeof(struct abc));
                  p->x = (int*)malloc(sizeof(int));
                  int i=10; 
                  p->x = &i; //or *p->x = 10; 
                  printf("The value is %d\n",*(p->x));
                  free(p);
          }
          

          问题是您将 值 10 分配给 指针,它把它当作一个地址而不是一个值,而您正试图打印使用 %d 的地址,因此编译器显示警告。要删除它首先需要取消引用结构的成员,即整数指针,现在您需要取消引用整数指针来分配值.或者你可以取一个整数并存储如下:

          p->x = &i.

          通过上面的结构体指针来访问结构体成员的指针是正确的方法。

          【讨论】:

          • 如果将i的地址存储在指针中,则无需为p-&gt;x分配内存。
          • 让他两种方式都试试..
          • 我是说当你在做 p-&gt;x=&amp;i 时,就不需要为 p-&gt;x 分配内存。然后取决于OP他想做什么。
          • 是的,这取决于 OP,这就是为什么我告诉他让他尝试两者。为此,我也编辑了我的答案。
          • 覆盖在p-&gt;x = &amp;i; 导致内存泄漏。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-12
          • 2017-03-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多