简体   繁体   English

使用双指针访问结构(指针)的成员

[英]access to a member of a struct (pointer) with the use of double pointer

hey I am trying to create a program in which I am trying store elements from one array to another with the use of a pointer to pointer but the problem is that is caused undefined behavior I believe that the problem is that I do not pass the elements in members with a proper way I know it is a vague way of doing this but It is in only for practising reasons嘿,我正在尝试创建一个程序,在该程序中我尝试使用指向指针的指针将元素从一个数组存储到另一个数组,但问题是导致未定义的行为我相信问题是我没有传递元素在以适当方式的成员中,我知道这是一种模糊的方式,但这只是出于实践原因

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 typedef struct student{
 
    char *name;
    int *number;    
 
 
 }T;
 
int main(void) {

char array[10][100]={"araaaa","bbgt","gffkghgh"};   

T arr[10][100];
T *p;
T **p1;
p=&arr[0][0];
p1=&p;


int i=0;

for(i = 0 ; i < 3 ; i++)
{   p=arr[i];
    strcpy((*p1)->name,array[i]);
    }

/*******print_elements*************/

for(i = 0 ; i < 3 ; i++)
{   p=arr[i];
    printf("\n the elements are %s",(*p1)-> name);
}


return 0;
}

When you do this:当你这样做时:

strcpy ((*p1)->name, array[i]);

(*p1)->name is an uninitialised pointer. (*p1)->name是一个未初始化的指针。 What happens, therefore, is in the lap of the gods.因此,所发生的一切都在众神面前。

The easiest fix is to modify your student structure such that name is a buffer, rather than a pointer.最简单的解决方法是修改您的student结构,使name是一个缓冲区,而不是一个指针。 At the same time, change number to an int , rather than a pointer to an int ::同时,将number更改为int ,而不是指向int ::

typedef struct student{
    char name [100];
    int number;    
} T;

If you want to keep name as a pointer then you have to allocate some memory before you store your string in it.如果要将name保留为指针,则必须在将字符串存储在其中之前分配一些 memory 。 This should work:这应该有效:

(*p1)->name = strdup (array[i]);

Don't forget to free the memory when done.完成后不要忘记free memory。

T is made of of two pointers, this first one points to a string of characters in memory. T由两个指针组成,第一个指针指向 memory 中的字符串。

arr is a 2D array that is allocated to store a total of 1000 T structures. arr是一个二维数组,分配用于存储总共 1000 个 T 结构。

arr[i] would reference a 1D array of T structures within arr arr[i]将引用arr中 T 结构的一维数组

*p1 would essentially be arr[i] , since dereferencing p1 gives you p , which was just set to arr[i] . *p1本质上是arr[i] ,因为取消引用 p1 会给你p ,它刚刚设置为arr[i] So, that is not a pointer to a T structure, but to an array of T structures.因此,这不是指向T结构的指针,而是指向T结构数组的指针。 Forcing the cast will likely give you a reference to the first T structure in that row, however.但是,强制转换可能会给您该行中第一个T结构的引用。

->name This value is never set. ->name永远不会设置此值。 You allocated an array, but "name" is a pointer to memory, not an array of characters, so '->name' is undefined.您分配了一个数组,但“名称”是指向 memory 的指针,而不是字符数组,因此“->名称”未定义。

I think you need to change arr to be a single dimension array.我认为您需要将arr更改为一维数组。 You aren't using 90% of it.您没有使用其中的 90%。

And, you need to initialize every T struct in that array.而且,您需要初始化该数组中的每个T结构。 You can use malloc or strdup , and then remember to free them all.您可以使用mallocstrdup ,然后记得将它们全部释放。 Or, set the struct to use an array instead.或者,将结构设置为使用数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM