so this is my code but it wont compile for some reason.
Error 3 error C2036: 'pjs *' : unknown size Error 4 error C2100: illegal indirection
Error 5 error C2037: left of 'size' specifies undefined struct/union 'pjs'
void initArray(struct pjs* array)
{
(*array[1]).size = 1;
}
struct pjs{
char* name;
int size;
int price;
};
int main(int argc , char** argv)
{
struct pjs array[10];
initArray(array);
system("PAUSE");
return (0);
}
那简直应该是
array[1].size = 1;
Following may help:
struct pjs{
char* name;
int size;
int price;
};
// safer to use one of the following declaration
// void initArray(pjs *array, std::size_t size) // simpler
// void initArray(pjs (&array)[10]) // more restrictive but unintuitive syntax
void initArray(pjs* array)
{
array[1].size = 1;
}
int main()
{
pjs array[10];
initArray(array);
}
pjs
should be given before to use it (or requiring its size). array[1]
is a pjs
so *array[1]
is illegal (as pjs
no have operator*
) Correct your 1st statement of initArray to :
array[1].size = 1;
and cut paste the struct declaration to before the function.
If you want to initialize the entire array, you need to pass the array and a size to initArray
.
int main(int argc , char** argv)
{
struct pjs array[10];
initArray(array, sizeof(array)/sizeof(array[0]));
system("PAUSE");
return (0);
}
and then, initialize each object the array as:
void initArray(struct pjs* array, size_t size)
{
for (size_t i = 0; i < size; ++i )
{
array[i].size = 1;
array[i].price = 0; // Or whatever makes sense
array[i].name = malloc(1); // Or whatever makes sense.
}
}
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.