结构指针的成员也是指针。 temp->data represent.Question 什么应该在代码中更清楚
member of a structure pointer is also a pointer. what should temp->data represent.Question is more clear in code
第一个打印的temp->data
是地址
而在后来的 print temp->data
变成值
*temp->data
什么都不打印
我试过 assining
值
int n = 5;
temp->data = &n;
然后 *temp->data 打印值 5
structure is
struct Node{
int *data;
struct Node *next;
};
/***part of code ***/
struct Node *temp = NULL;
temp = head;
while(count != l){
count++;
printf("%x %x %x ",temp,temp->data,&(temp->data));
printf("enter %d element\n",count);
scanf("%d",&(temp->data));
printf("%d\n",temp->data);
int *data;
创建一个未初始化的指针,并且必须指向有效的内存才能使用。 (如果不这样做会导致 未定义的行为 ,并且可能会出现段错误)
例如:
temp->data = malloc (sizeof *temp->data);
if (temp->data == NULL) {
perror ("malloc-temp->data");
/* handle error, e.g. return/exit */
}
现在您可以为 temp->data
分配一个整数值。 (不要忘记 free
不再需要时分配的内存)
假设您已经做到了(在其他一些代码中您没有做到 post),以下是错误的:
scanf("%d",&(temp->data));
temp->data
已经是一个指针,前面不需要'&'
。
(在您认为您的输入有效之前,您必须始终验证 scanf
return)
&temp->data
创建一个 指向指向 int
的指针(例如指向 data
的指针的地址)。使用 %x
打印指针值会导致未定义的行为,除非 sizeof (a_pointer)
和 sizeof (unsigned)
在您的系统上相同(不适用于 x86_64)。使用 %p
打印地址。
解决这些问题,如果您还有其他问题,请告诉我。
第一个打印的temp->data
是地址
而在后来的 print temp->data
变成值
*temp->data
什么都不打印
我试过 assining
值
int n = 5;
temp->data = &n;
然后 *temp->data 打印值 5
structure is
struct Node{
int *data;
struct Node *next;
};
/***part of code ***/
struct Node *temp = NULL;
temp = head;
while(count != l){
count++;
printf("%x %x %x ",temp,temp->data,&(temp->data));
printf("enter %d element\n",count);
scanf("%d",&(temp->data));
printf("%d\n",temp->data);
int *data;
创建一个未初始化的指针,并且必须指向有效的内存才能使用。 (如果不这样做会导致 未定义的行为 ,并且可能会出现段错误)
例如:
temp->data = malloc (sizeof *temp->data);
if (temp->data == NULL) {
perror ("malloc-temp->data");
/* handle error, e.g. return/exit */
}
现在您可以为 temp->data
分配一个整数值。 (不要忘记 free
不再需要时分配的内存)
假设您已经做到了(在其他一些代码中您没有做到 post),以下是错误的:
scanf("%d",&(temp->data));
temp->data
已经是一个指针,前面不需要'&'
。
(在您认为您的输入有效之前,您必须始终验证 scanf
return)
&temp->data
创建一个 指向指向 int
的指针(例如指向 data
的指针的地址)。使用 %x
打印指针值会导致未定义的行为,除非 sizeof (a_pointer)
和 sizeof (unsigned)
在您的系统上相同(不适用于 x86_64)。使用 %p
打印地址。
解决这些问题,如果您还有其他问题,请告诉我。