3

I've an array of pointers to structure. Can i initialize all them to NULL as below??

struct hash
{
    int bid;
    struct hash *prev,*next,*fl,*fr;
};

struct hash *h[4]={NULL,NULL,NULL,NULL};
1
  • 2
    That looks like valid syntax to me. Commented Jul 26, 2011 at 15:56

4 Answers 4

7

one NULL (or one 0) will be enough since 0 is converted to NULL when assigned to pointers

struct hash *h[4] = {0};

additional info: first element will be initialized to the first value supplied. rest will get 0 according to standards. which part, section of standards? i have no idea but you can find it somewhere in the standards.

Sign up to request clarification or add additional context in comments.

1 Comment

C11 6.7.9.19 says: all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration. and earlier in 6.7.9.10 says: If an object that has static or thread storage duration is not initialized explicitly, then: — if it has pointer type, it is initialized to a null pointer; — if it has arithmetic type, it is initialized to (positive or unsigned) zero; ...
1

Yes!

T *t[] = {NULL, NULL};

Works for each T.

Comments

1

You can also use

struct hash *h[]={0,0,0,0};

Comments

0

I think it will work

int main()
{
  struct hash
  {
    int bid;
    struct hash *prev,*next,*fl,*fr;
  };
struct hash *h[5]={0};

1 Comment

yeah there will be 0 not "NULL"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.