aralar
aralar

Reputation: 3120

Instantiating multiple structs in C?

What does the 100 specify in the following definition of a struct in C?

struct node_type 
{ float data;
  int next;
} node[100];

I wrote the following code to determine if it was forcing a specific struct size, but that's not the case (both returned a size of 8 bytes). Is it defining 100 instances of a node struct? If so, where are they being stored/how can they be accessed? Thanks!

#include <stdio.h>

struct node_type{
  float data;
  int next;
}node[100];

int main(){

  struct node_type node = {21.9, 2};

  printf("%ld\n", sizeof(node)); // 8
  printf("%ld\n", sizeof(struct node_type)); // 8

}

Upvotes: 0

Views: 1599

Answers (1)

Grzegorz Szpetkowski
Grzegorz Szpetkowski

Reputation: 37914

It simply means that you are declaring array of 100 elements, where each element is of type struct node_type.

Note that local node identifier shadows external one, so it's no longer an array in block scope of main function. There is no way in C to access node array, unless } punctuator, that represents end of this block scope is encountered.

Upvotes: 3

Related Questions