通过指针访问结构元素

时间:2013-01-08 07:12:30

标签: c pointers

我的程序有这样的结构:

struct point
{
  int x;
  int y;
}*ptt;

struct point *pointer(int c, int d)
{
  ptt->x = c;
  ptt->y = d;
  return ptt;
}
int main ()
{
  struct point *pqq;
  pqq = ptt;         // Not too sure about this
  pqq = pointer(100,123);
  printf("Pointer value is %d\n",pqq->x);
  return 0;
}

现在程序在调用指针时崩溃了。我怀疑我初始化x的方式和y ptt->x之类的错误是错误的...但我不太确定初始化它们的确切方法。这有什么问题?

5 个答案:

答案 0 :(得分:3)

如果不允许更改结构和指针功能,则main()中的小变化应该起作用

int main ()
{
 struct point *pqq;     

/* ptt is a global pointer visible to main and malloc returns a valid address of type struct point with out which you can not assign a value to its variable */
 ptt = malloc(sizeof(struct point));   

/* pqq = ptt is not necessary - the below statement does that already  */
 pqq = pointer(100,123);
 printf("Pointer value is %d\n",pqq->x);
 return 0; 
 }

答案 1 :(得分:1)

你应该在使用它们之前为指针分配内存,并在你不再需要使用指针时释放它们。请在代码中找到我的注释:

int main ()
{
  struct point *pqq=NULL;//Good practice to assign uninitialized pointers with a NULL
  //Before using the ptt pointer allocate memory
  ptt=malloc(sizeof(struct point));
  //Handle the memory allocation failed error
  ptt->x=ptt->y=0;//Good practice
  pqq = ptt;//both pqq and ptt point to same allocated address          
  pqq = pointer(100,123);//And this statement makes the earlier statement pqq=ptt useless. :)
  printf("Pointer value is %d\n",pqq->x);
  free(pqq);
  free(ptt);
  return 0;
}

希望这会对你有所帮助。

答案 2 :(得分:1)

使用如下:

int main ()
{
   struct point p;
   ptt = &p;
   struct point *pqq;
   pqq = pointer(100,123);
   printf("Pointer value is %d\n",pqq->x);

   return 0;
 }

您的代码在ptt-> x上显示错误,因为您正在使用指向结构变量“ptt”的指针而不进行初始化。你应该使用结构变量初始化它,以便它指向结构,然后你可以使用指针变量即ptt访问结构的成员。

答案 3 :(得分:1)

修复它的一种方法是这样的: 所以我发现的问题是,在你定义了支柱"点"你声明一个指向结构的指针" * ptt"但你没有初始化它。所以ptt什么也没说;让我们说ptt指向垃圾。所以在主要功能中进一步向下宣布" * pqq"并初始化为ptt指向的地址。所以你宣布" * pqq"并告诉它指向同一地址ptt指向此声明" pqq = ptt;"。但不要忘记ptt原本并没有指向任何东西。所以现在你有ptt和pqq指向垃圾。当你在这里调用这个功能时#34; pqq =指针(100,123);"该函数使用ptt直接访问其成员x和y。但ptt再次指出一无所获。所以该函数没有任何内容,因此没有返回pqq,也没有指向任何内容。程序崩溃,因为这是不允许的。因为您没有访问任何内容,所以您无法返回任何内容。所以我的修复是: 当您声明指向结构的指针时,在使用它之前动态初始化它。有固定代码和注释:

#include <stdio.h> // header that defines the standard input and output routines
#include <stdlib.h> // header to use malloc()

//struct definition
struct point
{
int x;
int y;
}*ptt = NULL;   // it's safe to first initialize pointers to NULL

//function definition
struct point *pointer(int c, int d)
{
ptt->x = c;
ptt->y = d;
return ptt;
}

int main()
{//main

   ptt = (struct point*) malloc(sizeof( struct point)); //malloc() creates a space big enough to hold a variable of type "struct point" this way " malloc(sizeof( struct point))".
                                                        // But malloc() returns a pointer to void (void*) as return value
                                                        // so i type cast that value returned by malloc()  into a pointer-to-struct this way "(struct point*)"
   struct point *pqq = NULL; // it's safe to first initialize pointers to NULL
   pqq = ptt;         //  now ptt points to the memory location given by the malloc function and pqq points to the same place as well
   pqq = pointer(100, 123);
   printf("Pointer value is %d\n", pqq->x);

   free(ptt); // don't forget to free memory. You only need to free one pointer. Whichever one of them.
              // Because both ptt and pqq point to the same memory location so by freeing one, you automatically free the other;
             // Don't attempt to free both at the same time because that would generate a run time error since the C language standard does not allow to free the same memory location twice.

   return 0;

}//end main

//Sorry for the long comment but i wanted to explain it in detail. Hope it helps.

答案 4 :(得分:0)

Online compiler SCreenshot你必须为指针分配内存。

 struct point
  {
      int x;
     int y;
  }*ptt;

 struct point *pointer(int c, int d)
 {
   ptt=malloc(10);
   ptt->x = c;
   ptt->y = d;
 return ptt;
}
int main ()
{
  struct point *pqq;        
 pqq = pointer(100,123);
 printf("Pointer value is %d\n",pqq->x);
 return 0; 
 }