1
struct packet_event *packet_event_p[8];

What does this mean ? Is it a pointer to an array of structure data type (struct packet_event) that has 8 elements ? And how could I make use of this pointer ?

Is it different from :

struct packet_event **packet_event_p;

If yes, how could I use this pointer ?

6
  • 3
    declare packet_event_p as array 8 of pointer to struct packet_event Commented May 17, 2016 at 7:38
  • 1
    Second one is: declare packet_event_p as pointer to pointer to struct packet_event Commented May 17, 2016 at 7:39
  • Add cdecl.org to your favorites bar. Seriously. Do it. First snippet, second snippet. Commented May 17, 2016 at 7:42
  • @WhozCraig That'a not a bad idea, do you think it's important to know these complicated declerations ? Commented May 17, 2016 at 7:47
  • 1
    @BohaoLI You career using C will be woefully cut short unless you can readily understand declarations like this pretty much on-sight. Commented May 17, 2016 at 7:48

3 Answers 3

1

The first one:

struct packet_event *packet_event_p[8];

stands for 'declare packet_event_p as an array of 8 pointers to struct packet_event'. Thus, you create an array packet_event_p of 8 elements, which are pointers to the struct packet_event. Please see this link.

whereas the second one:

struct packet_event **packet_event_p;

stands for 'declare packet_event_p as pointer to pointer to struct packet_event'. Please see this link.

Hope this is helpful.

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

7 Comments

So that the first pointer is a pointer to an array who has 8 elements ?
@BohaoLI, first is not actually a pointer per se. It is an array of pointers.
Bohao Li, think of it this way struct packaet_event *foo[x] creates an array of pointers (8 pointers), that you are free to use as you would any pointer and you can address them in array notation (e.g. foo[1], foo[2] and so on . struct packet_event **packet_event_p; creates a pointer-to-pointer-to-type struct packet. It is nothing more than 1 of them. However, you can allocate as many as you need, and realloc if you need more later. Each pointer you allocate here is a pointer to a newly allocated block of memory of type ans size struct packet_event. Let me know if you have q's.
@DavidC.Rankin Thanks. Another question : for the second pointer struct packet_event **packet_event_p, am I able to use it like using the first pointer ? Like packent_event_p[5] = .... And the difference between the two pointers is that : the first pointer has a size while the second doesn't. Is that ritght ?
@BohaoLI, yes, but you must allocate the pointers. In packent_event_p[5] you take advantage of the auto-allocation provided for statically allocated objects. Withstruct packet_event **packet_event_p, if you want the same 5 pointers, then you must do packet_event_p = malloc (sizeof *packet_event_p * 5); NOW you have **event_pack_p in the same state of readiness to accept address for each of the pointers as you do with, e.g. packent_event_p[5] that was statically allocated.
|
1

The first declaration :

struct packet_event *packet_event_p[8];

defines an array of 8 elements, each element of which is a pointer to struct packet_event . In other words, you have an array of 8 pointers, and each one points to a struct packet_event.

And how could I make use of this pointer ?

You can allocate memory for a struct packet_event and set a pointer of your array point to it, like this :

struct packet_event *ptr = malloc(sizeof(struct packet_event));
if (ptr == NULL)
    printf ("Error\n");
else
    packet_event_p[0] = ptr;    //choose a pointer packet_event_p[0] - packet_event_p[7]

The second declaration :

struct packet_event **packet_event_p;

is different, as you declare a pointer (and not an array), named packet_event_p, which points to a pointer to struct packet_event.

If yes, how could I use this pointer ?

Allocate memory for the double pointer packet_event_p. See this link for allocating memory for double pointers.

Comments

0

It is true that arrays can be decayed to pointers, but they are not the same.

regarding the type it is "Array of 8 pointers to a struct of packed event" reading the types in C usually goes in some sort of a whirlwind fashion. To properly to do, you can read this here.

Usually when you are passing this type as function argument, you will also add the size of the array, or use an external known variable to mark its length. Usually function declarations will be pointers instead of arrays type. I think even that the compiler does automatically (comments about that will be useful)

One different between those types can be seen using the sizeof operator. when applied on variable which is known to be an array type, then the result is the entire size of the array. Otherwise it will be a size of a pointer, which might be 4 byte or 8 byte (depending if its a 64/32bit machine).

Comments

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.