How to perform memset of a pointer variable of a struct which is a pointer var of another struct in C

1k views Asked by At

I have 2 structs:

struct A
{    
  B *b;
}a;

struct B
{
  int* Info;
} b;

How do I perform memset of info in C?

memset((a->b->Info,0,sizeof(int));

Info has to be memset with 0. This has to be done for 34 values. Can that be done through for loop?

Thanks in advance!

2

There are 2 answers

2
sanjeev mk On

When you define a struct, you're actually creating a user defined data type. In your code above, A is the datatype, a is the variable of that type. Same goes for B and b.

a.b -> Info is how you should access Info via A, in your case. . operator is used to access members of a stuct using a normal struct variable (non-pointer). If you defined a pointer x of type A, then you should use x->b->Info.

You can learn about C structures from here:

http://www.tutorialspoint.com/cprogramming/c_structures.htm

0
alk On

Define a and b in the right order and initialise them:

struct B
{
  int * Info;
} b = {
    NULL;
  };

struct A
{    
  B * b;
} a = {   
    &b
  };

Now a.b->info is NULL.