简体   繁体   中英

Error in C structure pointer

I'm not very good in C but I'm trying to do a hashtable that contains an array with linkedlist

But I have an error in this code:

function (&objectA.list) // Works
hashtable->tab[1]= &objectA; // I put my list into the array 
function (hashtable->tab[1].list) // Doesnt work

error:

request for member 'list' in something not a structure or union

The two code snippets are not doing the same thing: since . operator has higher precedence (in fact, it has the highest precedence of all operators) the expression

&objectA.list

means "the address of the list member of objectA .

Your second expression takes the address of objectA , and then tries to pull a list from it. That is not the same thing. Assuming that tab[1] is properly typed to hold a pointer of objectA , to make the second expression behave in the way the first expression does you need to write this:

function (&(hashtable->tab[1]->list));

Note: Although I added parentheses for clarity, they are not required, because -> operator is of the same precedence as the dot . operator.

As dasblinkenlight says, your first line of code doesn't do what you think, since . binds more tightly than & . You're taking the address of objectA.list , not accessing .list of &objectA .

In your last line of code, since you have a pointer, you want to use the indirect member operator,
-> , rather than the normal member operator, . . In other words, hashtable->tab[i]->list .

if tab[1] defined as a pointer to a structure then you can't use . operator with it as you did in

function (hashtable->tab[1].list) 

use -> instead, or (*hashtable->tab[1]).list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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