-4

While I am studying device driver I bump into this code below.

static struct char_device_struct{
    struct char_device_struct *next;
    ...
} *chrdevs[CHRDEV_MAJOR_HASH_SIZE];

I thought it could be defining struct array, so I wrote a simple code like this below and found strange things.

struct a{
    int _a;
} val_a[10];

struct b{
    int _b;
};

struct b val_b;

int main(){
    val_a->_a=10;
    val_b._b=10;

    return 0;
}

val_a is accessible by "->" like allocated memory in heap or mmap(accessing by pointer) but not accessed like ".".

  • what does it mean to use struct like array(e.g. CHRDEV_MAJOR_HASH_SIZE)
  • and if it is just defining struct array what is difference between just using like val_b.

Sorry for the bad English. Thank you very much.

JuHyung Son
  • 73
  • 2
  • 8
  • 7
    Please do not enter pure text as images to your questions. Just copy&paste text as text. – Gerhardh Jan 10 '18 at 07:52
  • 1
    `val_a->_a` is equivalent to `val_a[0]._a` due to implicit array to pointer conversion. That holds for any kind of array, not just arrays of `struct` – UnholySheep Jan 10 '18 at 07:53
  • `->` can be used with any pointer, even addresses of global/static or local variables. So your assumption that "like allocated memory in heap or mmap" is wrong. – Ajay Brahmakshatriya Jan 10 '18 at 08:52
  • @Gerhardh sorry. I removed images – JuHyung Son Jan 10 '18 at 09:01
  • @Stargateur I removed it. sorry.. – JuHyung Son Jan 10 '18 at 09:01
  • 1
    Possible duplicate of [Arrow operator (->) usage in C](https://stackoverflow.com/questions/2575048/arrow-operator-usage-in-c) – Stargateur Jan 10 '18 at 09:03
  • @Stargateur I think it is rather a problem of correct type definition than arrow usage. – Gerhardh Jan 10 '18 at 09:05
  • @Stargateur Thank you for the comment. But I was curious about defining struct like that is whether array of struct or not. – JuHyung Son Jan 10 '18 at 09:15
  • @JuHyungSon: Stack Overflow's voting system gives unexpected verdict sometimes. Before you have put code into the question post, you have collected 3 "off-topic" close votes. I agree with Stargateur in that core of your problem is understanding of operator `->`, so we both voted for duplicate. But since 3 close votes is more than 2, "off-topic" verdict wins, while it currently inapplicable to your question. Next time try to put basic things into the question post at the *creation time*, so you won't collect unneeded close votes (and downvotes). – Tsyvarev Jan 10 '18 at 09:22
  • @Tsyvarev I understand it. Thank you for your comment and advise. I agree with you. It was enough for me to someone answered my question to help me and teach me. Thank you so much. – JuHyung Son Jan 10 '18 at 09:31

1 Answers1

1

I thought it could be defining struct array,

No, it is not an array made of structs, but an array holding pointers to structs. The code defines an array holding CHRDEV_MAJOR_HASH_SIZE pointers without any memory for the structs itself.

In your code this would be

struct a{
    int _a;
} *val_a[10];

Then you can use it like this:

val_a[0] = malloc(sizeof(struct a));
val_a[0]->_a = 123;
(*val_a[0])._a = 123; // identical to previous line.

Or if you want to use the decayed pointer from that array:

*val_a = malloc(sizeof(struct a));
(*val_a)->_a = 123;

But this will only access the first element of the array.

Gerhardh
  • 11,688
  • 4
  • 17
  • 39