C - retrieving elements from a list
I'm very new in C and right now I'm stuck with a simple p开发者_Go百科roblem. I want to retrieve the i'th element of the list and I have the following API. The argument 'sp' specifies the pointer to the variable into which the size of the region of the return value is assigned.
const void *tclistval(const TCLIST *list, int index, int *sp);
The list contains uint64_t values. How do I retrieve the i'th element?
P.S. More details about that function:
The return value is the pointer to the region of the value. Because an additional zero code is appended at the end of the region of the return value, the return value can be treated as a character string. if 'index' is equal to or more than the number of elements, the return value is 'NULL'.
If you know the list contains uint64_t
elements, then it's just
int sp;
uint64_t elem;
elem = *(uint64_t *) tclistval(list, i, &sp);
sp
will contain the size of the object returned, which you already know so you don't have to worry about it.
You can also do error checking first:
uint64_t *pelem = tclistval(list, i, &sp);
if (pelem == NULL) { /* error! */ }
else
elem = *pelem;
精彩评论