im trying to make an array of struct and initialize struct array member, but I don't know how to access struct member, i used (st->ch)[t] = 'c'; and others similar syntax but i did not succeed.
best regards.
struct ST
{
char ch;
};
bool init(ST* st, int num)
{
st = (ST*)malloc(num * sizeof(ST));
if (st == NULL) return false;
for (int t = 0; t < num; t++) (st->ch)[t] = 'c';
return true;
}
int main()
{
ST* s = NULL;
init(s, 2);
putchar(s[1].ch);
}
You declared in main a pointer
that in C shall be declared like
because you declared the type specifier
struct ST(that in C is not the same as justST)that you are going to change within a function. To do that you have to pass the pointer to the function by reference. That is the function declaration will look at least like
and the function is called like
The function itself can be defined like
If you are using a C++ compiler then substitute this statement
for
When the array of structures will not be needed you should free the memory occupied by the array like