1

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);
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Sofia
  • 55
  • 7
  • 5
    `st[t].ch = 'c';` but that only fixes the syntax error. There is still a big logic error. `st` is a local variable in the function. Changing it does not change the caller's `s` variable - it will still be `NULL`. See: [Changing address contained by pointer using function](https://stackoverflow.com/questions/13431108/changing-address-contained-by-pointer-using-function). – kaylum Aug 10 '21 at 11:06

3 Answers3

1

You can access struct member using following:

st[t].ch
ShahzadIftikhar
  • 515
  • 2
  • 11
1

You declared in main a pointer

ST* s = NULL;

that in C shall be declared like

struct ST* s = NULL;

because you declared the type specifier struct ST (that in C is not the same as just ST)

struct ST
{
    char ch;
};

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

bool init( struct ST **st, int num );

and the function is called like

init( &s, 2);

if ( s ) putchar( s[1].ch );

The function itself can be defined like

bool init( struct ST **st, int num )
{
    *st = malloc( num * sizeof( struct ST ) );

    if ( *st )
    {
        for ( int i = 0; i < num; i++) ( *st )[i].ch = 'c';
    }

    return *st != NULL;
}

If you are using a C++ compiler then substitute this statement

    *st = malloc( num * sizeof( struct ST ) );

for

    *st = ( struct ST * )malloc( num * sizeof( struct ST ) );

When the array of structures will not be needed you should free the memory occupied by the array like

free( s );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

As mentioned by @kaylum st being a local variable in the init() and doesn't update the variable s in the main function hence an alternative could be you either pass the add the address of the variable s to init() or could just return the allocated memory as shown in the code snippet below. Instead of using bool as return type to check you can use the ST* as return type and if it returns NULL or mem address to get mem alloc status.

Also you would have to typedef the struct typedef struct ST ST; to be able to directly use the type as ST else you would have to stick to using struct ST

typedef struct ST
{
    char ch;
}ST;

ST* init(int num)
{
  ST *st;

  // Create num elems of ST type
  st = (ST*)malloc(num * sizeof(ST));

  // return NULL is st unintialised
  if (st == NULL) {
    return st;
  }

  // Assign ch member variable of the 't'th st element wit 'c'
  for (int t = 0; t < num; t++) {
    st[t].ch = 'c';
  }

  return st;
}

int main()
{
  ST* s;

  // creates an array of size two of type st
  s = init(2);

  putchar(s[1].ch);

  return 0;
}