我正在尝试执行以下操作:
struct LinkedListStruct
{
void* this;
LinkedList * next;
};
struct GraphLinkedList
{
int V;
int E;
LinkedList** adjlist;
};
/*
* Function:
* getItemLinkedList
*
* Description:
* Gets the item of a linked list node.
*
* Arguments:
* Pointer to a linked list node:
* (LinkedList *) node
*
* Return value:
* Returns the pointer to the item of a linked list node. NULL
* is returned if the node is NULL (or if the item is NULL).
*/
Item getItemLinkedList(LinkedList * node)
{
/* Check if node is not empty */
if(node == NULL)
return NULL;
return node->this;
}
/*
* Function:
* insertUnsortedLinkedList
*
* Description:
* Creates a new linked list node.
*
* Arguments:
* Item to associate to the new node:
* Item this
* Pointer to the next node:
* (LinkedList *) next
*
* Return value:
* Returns the pointer to the node.
*/
LinkedList * insertUnsortedLinkedList(LinkedList * next, Item this)
{
LinkedList * new;
/* Memory allocation */
new = (LinkedList *) malloc(sizeof(LinkedList));
/* Check memory allocation errors */
if(new == NULL)
return NULL;
/* Initialize new node */
new->this = this;
new->next = next;
return new;
}
GraphLinked* GraphInit (int V, int E)
{
GraphLinked* G=NULL;
int i=0;
int init[2]={-1,0};
G= (GraphLinked*) calloc(1, sizeof(GraphLinked));
G->V=V;
G->E=E;
G->adjlist=(LinkedList**) calloc(V, sizeof(LinkedList*));
for(i=0; i<V; i++)
{
G->adjlist[i]=insertUnsortedLinkedList(NULL, (Item)init);
}
return G;
}
void EdgeAdd (GraphLinked* G, int edge[3])
{
int info[2]={0};
LinkedList* new=NULL;
info[0]=edge[1];
info[1]=edge[2];
new=insertUnsortedLinkedList(G->adjlist[edge[0]]->next, (Item) info);
G->adjlist[edge[0]]=new;
info[0]=edge[0];
new=insertUnsortedLinkedList(G->adjlist[edge[1]]->next, (Item) info);
G->adjlist[edge[1]]=new;
}
void ReadtoList (GraphLinked *G, FILE *input)
{
int edge[3]={0};
int i=0;
for(i=0; i<G->E; i++)
{
if(fscanf(input, "%d %d %d", &edge[0], &edge[1], &edge[2])!=3)
exit(0);
EdgeAdd(G, edge);
}
}
void PrintList (GraphLinked *G, FILE *output)
{
LinkedList *aux=NULL;
int i=0;
fprintf(output, "%d\n", G->V);
for(i=0; i<G->V; i++)
{
aux=G->adjlist[i];
while(aux!=NULL)
{
fprintf(output, "%d:%d\n", (int)(aux->((int*)this[0])), i);
aux=aux->next;
}
}
}
唯一相关的函数是最后一个。 在这一行中:
fprintf(output, "%d:%d\n", (int)(aux->((int*)this[0])), (int)(aux->((int*)this[1])));
我收到错误:“错误:访问结构时‘(’标记之前有预期标识符” 这可能是我没有得到的语法问题。有人可以帮助我吗?
最佳答案
fprintf(output, "%d:%d\n", (int)(aux->((int*)this[0])), (int)(aux->((int*)this[1])));
也许你的意思是这样的:
fprintf(output, "%d:%d\n", ((int*)(aux->this))[0], ((int*)(aux->this))[1]);
您不能在 ->
和成员名称之间放置任何内容。表达式 aux->this
的类型为 void *
。您想要将 this
成员强制转换为 int*
,您可以这样做:(int*)aux->this
。如果您知道 aux->this 指向整数数组,并且想要获取该数组中第二个元素的值,则可以执行 ((int*)aux->this) [1]
.
供引用:member access operator 。 ->
是一个成员访问运算符,它需要一个表达式
(例如声明的变量名称)和一个member_name
(字面意思是名称,一个 identifier )。您不能在 ->
之后、member_name
之前放置任何内容,s->(int*)member_name
无效。 cast operator优先级低于 ->
,因此编译器将首先执行 ->
运算符,然后进行强制转换,即: (int*)(aux->this)
与 (int*)aux->this
相同。运算符从左到右求值,具有相同优先级的运算符被分组在一起,因此在表达式 (int*)aux->this[1]
运算符 ->
和 []
将被分组在一起并从左到右评估,因此首先将评估 ->
,然后评估 []
,然后进行强制转换。因此 (int*)aux->this[1]
与 (int*)(aux->this[1])
相同,请参阅 operator precedence 。您可能想最后评估 []
,因此我们需要额外的大括号 ((int*)aux->this)[1]
。
关于c - "Error: expected identifier before '(访问结构时为' token",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50369858/