Complete.Org: Mailing Lists: Archives: discussion: February 2000:
[aclug-L] Re: Simple C question (which shows my C ignorance)
Home

[aclug-L] Re: Simple C question (which shows my C ignorance)

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: aclug-L@xxxxxxxxxxxx
Subject: [aclug-L] Re: Simple C question (which shows my C ignorance)
From: Greg House <ghouse@xxxxxxxxxxxxx>
Date: Thu, 3 Feb 2000 23:15:31 -0600
Reply-to: aclug-L@xxxxxxxxxxxx

On Thu, 03 Feb 2000, you wrote:
> On Thu, 3 Feb 2000, Larry Bottorff wrote:
> 
> Let me simplify things, make sure I understand what's going on:
> 
> > I do a malloc for each member of the array (of stuctures):
> > array1[count] = ... as I need it after declaring
> > array1[NUM] beforehand. Then I need to remove a given member of array1.
> 
> > If I shift everything down, how do I use realloc?
> 
> skip the realloc and just use a for() loop. Loop from the one you
> want deleted until NUM-1; assign [i] = [i+1]. Decrement NUM by 1.

Better yet, use a linked list instead of the array of pointers. Then you just 
change the 
previous/next pointers for the list elements before and after the one you're
deleting and free the memory for it. You don't have to resequence the entire
rest of your array to remove a pointer from the middle of it.

Greg


[Prev in Thread] Current Thread [Next in Thread]