'using a string to reference a struct
I'm having troubles with a function in C using a string to reference a struct, the used code is included below
int Search_name (char nombre[],Nentrada user_number){
int result;
int i;
for (i = 0; i < user_number ;i++)
{
char *temp;
asprintf(&temp,"user%d",i);
printf("%s \n",temp);
result = strcmp(nombre,user1.Nombre);
}
if (result != 0)
{
result = 1;
}
return result;}
The code is used in a function that is usposed to search for a string in the argument "nombre" of the struct "entrada", it works perfectly when i manually use the struct assignments "user1.Nombre, user2.Nombre..." nonetheless when I use the string temp (which switch between values user1,user2...) I keep receiving the error
error: request for member 'Nombre' in something not a structure or union|
Therefore I would like to know how can I aumatically switch the struct without having the issue.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|