Reputation: 245
For the following example (from mysql source code), it uses Field **field. I am in trouble to think it as a 2 dimension array of Field.
typedef struct st_table_share
{
.......
Field **field;
....
}
for (Field **field=table->field ; *field ; field++)
{
...
Can I think it in this way, a table contains many rows and a row contains multiple columns/fields. So *field means a row, and **field mean a table?
If that's true, for the following code
for (Field **field=table->field ; *field ; field++)
it will exit when *field is null, so how could *field be null if *field is a row. Or can I say if a row has 5 columns, and field is the first column, then field+4 is the last column field, and field+5 is the end of the column which means null, so that the for loop will exit?
Upvotes: 3
Views: 1655
Reputation: 27278
Or can I say if a row has 5 columns, and field is the first column, then field+4 is the last column field
Yes, but, you can say that
and field+5 is the end of the column which means null
only if you initialized the array to be null-terminated.
And if you know how many columns your table has, you can iterate until you reach the last column instead of ; *field ;
.
Upvotes: 0
Reputation: 87531
Are you sure that Field **
is a two-dimensional array?
A variable of type Field **
does not necessarily represent a two-dimensional array of Fields. It could be a pointer to a one-dimensional array of pointers to Fields. Storing an array of pointers could be more convenient than storing an array of structs for several reasons: you can allocate the memory for the structs independently, you don't have to use memcpy to add a struct to the list, and certain list operations will be less expensive. In fact, this is done in libusb, where a variable of type libusb_device **
represents a (one-dimensional) list of usb devices.
Upvotes: 1
Reputation: 363848
So
*field
means a row, and**field
mean a table?
In declaration syntax, yes.
how could
*field
be null if*field
is a row
If the authors of the code take care to preserve the invariant that field
is terminated by a null pointer, as is done with argv
. I.e., a table of n rows is an array of n+1 pointers, the last of which must always be null. This is a common C convention that obviates the need for an explicit count.
Upvotes: 4
Reputation: 5154
The loop will continue until "*field" is null. i.e. the array should have a null-terminator (a non-existing row) at the end.
Upvotes: 0