Snažím sa implementovať zásobník a jeho základné vlastnosti (push, pop, atď.), Ale ja som sa dostal chybu, ktorú som uviedol v názve:
CHYBA: požiadavka na členský reťazec Dĺžka a názov v niečom, čo nie je štruktúra alebo odbor
#include <stdio.h>
#include <stdlib.h>
typedef struct stackElement
{
int stringLength;
char *name;
} StackElement;
int Push(StackElement **stack);
int main()
{
StackElement *stack = NULL;
int index = 0;
index = Push(&stack);
printf("The top word of the stack is %sn", stack[index].name);
system("PAUSE");
return 0;
}
int Push(StackElement **stack)
{
char *c;
int size = 0;
int i = 0;
*stack = malloc(sizeof(StackElement));
printf("Please enter a word in the stackn");
scanf("%s",&c);
size = sizeof(c)/sizeof(char);
*stack[i].stringLength = size;// <---- ERROR
*stack[i].name = c ;// <----- ERROR
return i;
}
odpovede:
4 pre odpoveď č. 1Je to otázka priority operátora.
.
má vyššiu prioritu než *
, takže:
*stack[i].stringLength
Je to isté ako
*(stack[i].stringLength)
Kým skutočne chcete
(*stack[i]).stringLength
Stačí pridať zátvorky, ako som to urobil v poslednom príklade, a malo by to fungovať.