LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-12-2006, 06:09 PM   #1
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
Standard C library numeric array functions?


Hello all,

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.

Thanks.
 
Old 04-12-2006, 06:33 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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...
 
Old 04-12-2006, 07:02 PM   #3
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760

Original Poster
Blog Entries: 4

Rep: Reputation: 78
Quote:
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?

Last edited by bulliver; 04-12-2006 at 07:07 PM.
 
Old 04-12-2006, 07:14 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.)

graeme
 
Old 04-12-2006, 07:36 PM   #5
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760

Original Poster
Blog Entries: 4

Rep: Reputation: 78
Quote:
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...
 
Old 04-12-2006, 07:41 PM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
The pseudo code might be like this
Code:
remove (int ele)
for (int i = ele ; i < ArrSize; i++)
   myArray[i] = myArray[i+1]
--ArrSize;
Where ele is the element number to be removed, in the example I gave above it would be 3
 
Old 04-12-2006, 07:49 PM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 04-12-2006, 08:18 PM   #8
tamoneya
Member
 
Registered: Jan 2005
Location: MA
Distribution: Ubuntu 7.10
Posts: 558

Rep: Reputation: 31
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.
 
Old 04-12-2006, 08:45 PM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Sorry I should of made it clearer, I was worried about the following statement:
Code:
int x = atoi(argv[1]);
unsigned long set[x];
That was to be used to create the array
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting Functions from a Shared Library TGWDNGHN Linux - Newbie 10 12-23-2018 08:48 PM
using arrays and functions and trying to initialize a point in the array mshinska Programming 1 11-11-2005 02:21 AM
array functions in c djgerbavore Programming 6 01-07-2005 03:28 PM
php: array with functions ldp Programming 7 09-22-2004 04:55 PM
C++ standard library. Thetargos Programming 8 10-09-2003 03:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration