[英]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.