|
|
View previous topic :: View next topic |
Author |
Message |
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
Correct Terminology |
Posted: Wed Nov 09, 2005 8:05 am |
|
|
Hello All,
I'm trying to find an "off-the-shelf" function to repack? an array after I remove an item. I haven't had much success with the term "repack", what terminology should I use in my search.
I thought it was going to be a quick find.... so I didn't write it from scratch. I guess I'll start now and see what you guys can suggest.
Thanks,
John |
|
|
MikeValencia
Joined: 04 Aug 2004 Posts: 238 Location: Chicago
|
|
Posted: Wed Nov 09, 2005 8:25 am |
|
|
Do you mean, if you have array 1,2,3,4,5, and then you want to remove '3', then the array should contain 1,2,4,5,'null'?
I've done it in the past with linked lists in programmer interviews. I forgot way too much since then.
You might want to draw the steps out by hand, then code it.
It looks like you would go to the Nth item that you want to remove, then use a loop to copy the contents of the (N+1)th into the previous.
Something like
while (n < ARRAY_SIZE)
{
my_array[n] = my_array[n+1]
n++
} |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Wed Nov 09, 2005 8:43 am |
|
|
Thanks Mike,
Yes, requirement is to have 1,2,3,4,5,null,null,null.
Remove 3 and then have 1,2,4,5,null,null,null,null.
I'll start pounding it out in a few minutes when I get done with present task.
Thanks again,
John |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 09, 2005 10:13 am |
|
|
Quote: |
I'm trying to find an "off-the-shelf" function to repack? an array after I remove an item. I haven't had much success with the term "repack",
what terminology should I use in my search. |
Look at string or memory handling functions.
String functions start with "str" and memory functions start with "mem".
These are in the CCS manual. Look at the chart on pages 86 and 87
for a list of these functions.
You won't find a "repack" function there. You will find functions that
can move strings or move contiguous blocks of memory. They can
be used to solve your problem.
For more information, go to Google and search on this string:
C string handling functions |
|
|
MikeValencia
Joined: 04 Aug 2004 Posts: 238 Location: Chicago
|
|
Posted: Wed Nov 09, 2005 10:33 am |
|
|
I did the following program in Unix and got the following results. It may not translate verbatim to the PIC though.
Code: |
#include <stdio.h>
void myremove(int position);
int myarray[8];
void main(void)
{
int x;
for (x=0; x<8; x++)
{
myarray[x] = x;
}
printf ("Initial contents:\n");
for (x=0; x<8; x++)
{
printf ("myarray[%d] = %d\n", x, myarray[x]);
}
myremove(2); // I'll take out the 2nd
printf("After:\n");
for (x=0; x<8; x++)
{
printf ("myarray[%d] = %d\n", x, myarray[x]);
}
}
void myremove(int position)
{
int n;
n = position;
while (n < 7)
{
myarray[n] = myarray[n+1];
n++;
}
// Now go to the last element and turn it into a null
myarray[7] = '\0';
}
hawk:/usr2/student/v/valemic1>
|
"ccspost.c" 46 lines, 674 characters
hawk:/usr2/student/v/valemic1>!gcc
gcc ccspost.c -o ccspost
ccspost.c: In function `main':
ccspost.c:8: warning: return type of `main' is not `int'
hawk:/usr2/student/v/valemic1>./ccspost
Initial contents:
myarray[0] = 0
myarray[1] = 1
myarray[2] = 2
myarray[3] = 3
myarray[4] = 4
myarray[5] = 5
myarray[6] = 6
myarray[7] = 7
After:
myarray[0] = 0
myarray[1] = 1
myarray[2] = 3
myarray[3] = 4
myarray[4] = 5
myarray[5] = 6
myarray[6] = 7
myarray[7] = 0
hawk:/usr2/student/v/valemic1> |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|