2

我的程序有这样的结构:

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是错误的。但我不太确定初始化它们的确切方式。这里有什么问题?

4

5 回答 5

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; 
 }
于 2013-01-08T07:47:09.417 回答
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;
}

希望这对您有所帮助。

于 2013-01-08T07:39:54.720 回答
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访问结构的成员。

于 2013-01-08T11:36:09.267 回答
1

修复它的一种方法是这样:所以我发现的问题是,在你定义了支柱“点”之后,你声明了一个指向结构的指针“*ptt”,但你没有初始化它。所以ptt什么都没有;假设ptt指向垃圾。因此,在主函数的下方,您声明“*pqq”并初始化为 ptt 指向的地址。所以你声明“*pqq”并告诉它指向相同的地址ptt指向这个语句“pqq = ptt;”。但是不要忘记 ptt 最初并没有指向任何东西。所以现在你有 ptt 和 pqq 都指向垃圾。当你在这里调用函数时“pqq = pointer(100,123);” 该函数使用 ptt 直接访问其成员 x 和 y。但是 ptt 再次指向什么。所以该函数没有访问任何内容,因此没有返回任何内容到 pqq 中,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.
于 2015-05-26T09:03:13.273 回答
0

在线编译器截图您必须为指针分配内存。

 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; 
 }
于 2013-01-08T07:19:33.047 回答