Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Pointer Type

7 views
Skip to first unread message

polas

unread,
Dec 15, 2008, 9:14:23 AM12/15/08
to
Hi all - I have a quick question. I want to store memory pointers in a
list. The list doesn't know the type of data that the pointer points
to, it just needs to know the pointer value.

What is the correct/best type to use for the pointer. I was thinking
ptrdiff_t as defined in <stddef.h> However, is there a better (or
correct) type to use.

Thanks,
Nick

Lew Pitcher

unread,
Dec 15, 2008, 9:20:39 AM12/15/08
to
On December 15, 2008 09:14, in comp.lang.c, polas (ni...@helpforce.com)
wrote:

> Hi all - I have a quick question. I want to store memory pointers in a
> list. The list doesn't know the type of data that the pointer points
> to, it just needs to know the pointer value.
>
> What is the correct/best type to use for the pointer.

void *

> I was thinking ptrdiff_t as defined in <stddef.h>

Not a good choice at all. ptrdiff_t is not a pointer type; it is the type
given to the delta between two pointers of the same type
(i.e. ((char *) - (char *)) ).

> However, is there a better (or correct) type to use.

For anonymous pointers, use
void *

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------


pete

unread,
Dec 15, 2008, 10:35:52 AM12/15/08
to

Use this type of node:

struct list_node {
struct list_node *next;
void *data;
};

llsort is a stable sort function
which uses a linked list.

It's here:

http://www.mindspring.com/~pfilandr/C/e_driver/e_sort.c

int
llsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));

It's just like qsort except that it doesn't
sort if malloc returns a null pointer
and it returns a status.

There's a lot of static helper functions that go with it.

int
llsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
char *array;
char *after;
list_type *head;
list_type *tail;

int rc = 1;
array = base;
after = array + size * nmemb;
tail = head = NULL;
while (array != after) {
tail = list_append(&head, tail, array, size);
if (tail == NULL) {
list_free(head, free);
head = NULL;
rc = 0;
break;
}
array += size;
}
array = base;
tail = head = list_sort(head, compar);
while (tail != NULL) {
memcpy(array, tail -> data, size);
array += size;
tail = tail -> next;
}
list_free(head, free);
return rc;
}

--
pete

0 new messages