[英]how to pass a pointer to a pointer to a struct member?
因此,当前的问题是将格式为YYYYMMDD的数字字符串转换为其他结构中的struct tm类型成员。 事实是,我真的只关心获得具有合理值的struct tm。
考虑以下结构:
typedef struct some_node {
char somestring[64];
char anotherstring[128];
struct tm date_one;
struct tm date_two;
int some_val;
struct some_node *prev;
struct some_node *next;
} some_node_t;
在里面,我有两个来自time.h标头的struct tm类型的成员。 似乎很合理。 也有指针成员在其中以创建链接列表,但这不是问题。
因此,我在尚未创建的链表中创建了第一个节点,如下所示:
/* begin the linked list of some_node_t */
struct some_node *t_head =
calloc( (size_t) 1, sizeof( some_node_t ) );
if ( t_head == NULL ) {
/*Memory allocation fault */
printf ( " FAIL : Memory allocation fault at %s(%d)\n",
__FILE__, __LINE__ );
exit ( EXIT_FAILURE );
}
/* I used calloc above which zero fills memory so these
* next lines are not really needed. Better safe than sorry. */
t_head->some_val = 0;
t_head->prev = NULL;
t_head->next = NULL;
然后,我可以将char数据填充到两个char成员中:
strcpy ( t_head->somestring, "birthday" );
strcpy ( t_head->anotherstring, "19981127" );
没问题。
在函数中,将字符串转换为struct tm似乎很合理,因为我可能必须做两次。
因此,我这样写:
int timestr_to_tm ( struct tm **date_val, char *yyyymmdd ) {
/* assume 8 digits received in format YYYYMMDD */
int j, date_status = -1;
char yyyy[5]="0000";
char mm[3]="00";
char dd[3]="00";
/* copy over the year digits first */
for ( j=0; j<4; j++ )
yyyy[j]=yyyymmdd[j];
/* month digits */
mm[0]=yyyymmdd[4];
mm[1]=yyyymmdd[5];
/* day digits */
dd[0]=yyyymmdd[6];
dd[1]=yyyymmdd[7];
*(date_val)->tm_year = atoi(yyyy) - 1900;
*(date_val)->tm_mon = atoi(mm) - 1;
*(date_val)->tm_mday = atoi(dd);
*(date_val)->tm_hour = 0;
*(date_val)->tm_min = 0;
*(date_val)->tm_sec = 0;
*(date_val)->tm_isdst = -1;
return 0;
}
因此,我的希望是,我可以将一个指向t_node中成员date_one的指针的指针传递给该函数。
if ( timestr_to_tm ( &(t_node->date_one), "19981127" ) < 0 ) {
/* deal with a bad date conversion */
}
好吧,我的编译器适合这里。 声明:
error: argument #1 is incompatible with prototype:
也许我应该有&t_head-> date_one,但我认为指针取消引用运算符“->”优先于“地址”运算符。 甚至试图将指针传递给结构中的成员可能是个坏策略?
更糟糕的是,在函数timestr_to_tm()中,我得到了:
error: left operand of "->" must be pointer to struct/union
在这些行中,我尝试将值分配给struct tm变量。
我在没有传递指针的情况下尝试了所有这些操作,但是该过程正常运行,但是返回时在struct tm成员中没有任何内容。 所以我想知道,我在这里想念什么?
我认为指针取消引用运算符
->
优先于“地址”运算符
确实如此,在取消引用*
运算符上也是如此。 因此,例如:
*(date_val)->tm_mday = atoi(dd);
应该
(*date_val)->tm_mday = atoi(dd);
但是:您为什么要这样做? 为什么不将指针传递给struct tm
,并使用->
而没有更高级别的间接访问?
如果您确实想要将指针传递给指向结构tm的指针,则可以,只需创建一个指针变量来保存指针并将指针传递给该指针:
struct tm *pointer = &t_node->date_one;
if ( timestr_to_tm ( &pointer, "19981127" ) < 0 ) {
...
问题是为什么? 您不需要额外的间接级别,因为您没有尝试更改指针,您只想在节点内填充struct tm。 因此,只需使用一个指针:
int timestr_to_tm ( struct tm *date_val, char *yyyymmdd ) {
:
date_val->tm_year = atoi(yyyy) - 1900;
:
那么您可以使用一个简单的指针来调用它,而无需创建一个额外的指针变量:
if ( timestr_to_tm ( &t_node->date_one, "19981127" ) < 0 ) {
...
由于我太懒了,无法理解这个问题,所以我只添加这个不相关的示例,希望可以帮助清理问题。 如果使用正确的语法和间接级别,这将非常简单。
typedef struct _node node_t;
struct _node {
node_t* next;
int a;
};
void fill_in_int(int* pointer_to_int) {
*pointer_to_int = 42;
}
void populate_all_nodes(node_t* list, int a_value) {
node_t* node;
for (node=list; node; node = node->next) {
fill_in_int( &(node->a) ); // Pass pointer to member a in node
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.