Re: problem passing pointer array
- From: pereges <Broli00@xxxxxxxxx>
- Date: Mon, 30 Jun 2008 09:39:56 -0700 (PDT)
On Jun 30, 9:33 pm, pete <pfil...@xxxxxxxxxxxxxx> wrote:
I would write that this way:
/* BEGIN new.c */
#include <stdio.h>
#include <stdlib.h>
int *create_ptr_list(int *a, size_t n, size_t *size_ptr)
{
int *b;
size_t i;
*size_ptr = 0;
for (i = 0; n > i; ++i) {
if (40 > a[i]) {
++*size_ptr;
}
}
b = malloc(*size_ptr * sizeof *b);
*size_ptr = 0;
if (b != NULL) {
for (i = 0; n > i; i++) {
if (40 > a[i]) {
b[(*size_ptr)++]= a[i];
}
}
}
return b;
}
int main(void)
{
int a[] = {5, -6, 45, -100, 20, -150, 160, 40, 0, 0, 1};
int *b;
size_t size;
size_t i;
b = create_ptr_list(a, 10, &size);
if (b != NULL) {
for (i = 0; size > i; ++i) {
printf("%d\n", b[i]);
}
} else {
puts("b == NULL");
}
free(b);
return 0;
}
/* END new.c */
I actually need an array of pointers. The array b will contains
pointers to elements in a which are less than 40.
The distinction between counters and sortable data
is more obvious with size_t counters.
I don't like to read sorting functions
where everything is int.
Sometimes using size_t or any unsigned entity can cause trouble in
sorting algorithms depending on how we write the sorting algorithm.
eg. just check the post i made on a quick sort algo today.
.
- Follow-Ups:
- Re: problem passing pointer array
- From: pete
- Re: problem passing pointer array
- From: pete
- Re: problem passing pointer array
- References:
- problem passing pointer array
- From: pereges
- Re: problem passing pointer array
- From: pete
- problem passing pointer array
- Prev by Date: Re: OOP
- Next by Date: Re: OOP
- Previous by thread: Re: problem passing pointer array
- Next by thread: Re: problem passing pointer array
- Index(es):
Relevant Pages
|