ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am just trying to play around with some C here, integer arrays in particular. I am running into difficulty because I cannot find any functions that deal with integer arrays in the glibc docs.
Specifically, I want to step through an array from beginning to end. The problem is I don't know how long my array is at compile time. I have two books on C here, and both of them use a fixed length arrays in their example code which makes it trivial to iterate over them using a for loop. What do you do if you don't know the length? I have found that:
Code:
int arraylength = ((int)sizeof array / 4);
sort of works, because an int is four bytes, but this seems really tacky, and non-portable. All the array functions in the glibc docs assume you have a char array. Is there a numeric array equivalent to '\0'?
Any pointers to example code, docs, or anything would be appreciated.
I know I haven't used C for a few yrs, but when I did (for a few yrs) I seem to remember that one slight inconvenience was that (in std C anyway) array lengths have to be declared at compile time.
I'm open to correction if anyone knows better...
array lengths have to be declared at compile time.
Right, sure. I guess I need to explain more. The length of the array is decided by the user by command line argument, so:
Code:
int x = atoi(argv[1]);
unsigned long set[x];
where 'set' is my array.
Here is some psuedocode for what I am trying to do:
Code:
while (test set[0]) {
for (each set member) {
if (a different test on set[member]) {
remove set[member]
reset 'set' indices
}
}
shift set[1] >> set[0]
}
Initializing and populating the array isn't a problem, and even the first iteration of the while loop is no prob because I know the size of the initial array, but after removing items from it I no longer know the size.
This begs another question: are there functions to remove arbitrary array members and reset the indices, or do I have to use a temporary array to keep the numbers that pass the test, then overwrite set with the temp array?
As I understand your problem it is just a case of keeping track of the size of the array. You have an initial size ArySize, you remove an element from the array, so the size is --ArySize
If you wish to remove an element there are a couple of ways of doing it, but you will have to do it. One could be to use a temp as you suggested another could be to just overwrite the element to be removed and shuffle everything to the left, hence:
myArray [1,4,6,5,8,9,3,7]
ArySize = 8
now remove the 5
myArray [1,4,6,8,9,3,7,7]
ArySize = 7
(and so the final element is ignored.)
As I understand your problem it is just a case of keeping track of the size of the array. You have an initial size ArySize, you remove an element from the array, so the size is --ArySize
Of course! Thanks. I guess I just thought finding the length of an array would be a common enough task there would be a standard function for it.
Quote:
another could be to just overwrite the element to be removed and shuffle everything to the left,
Ok, I like this idea, but I don't understand your example. Where is the extra 7 coming from? How are you removing the 5? How do you 'shuffle' the elements left?
I have found some code:
Code:
memmove(array, array+1, sizeof(int) * 4);
which will do the 'shift set[1] >> set[0]' which I need, but I am unsure how to use it do remove an element in the middle of an array...
Okay now for one concern I have. An array in C is declared with a constant size see. I suspect that what you will get with you above example (if it compiles) is an array that will stomp all over your memory and one day you will get a segmentation fault and be quite sad.
i dont see how it could mess up your memory if you are making it smaller. But if you are really worried abotu that you should jst have it make a new array with a new size and copy the elements you want to keep into and then change the pointer. Then repeat your loop or whatever.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.