473,397 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How to traverse the items in an array ?

I need a function that iterates over an array, like this one:

int iterate_array (void* mem, size_t size, callback func)
{
char* p = (char*)mem;
while(size-- 0) {
func(p);
p += size;
}
}

but i'm not sure how to get the value of 'size'. First of course was
the idea

int a[10];
iterate_array(a, sizeof(int), intfunc)

but what if the data is 6 byte? Then its up to the compiler (or
pragmas) it might be aligned to 8. How can i find this alignment. Will
a (sizeof(type[2])/2) expression always return the correct size
including alignment so that the generic iterator works ?

Apr 23 '07 #1
7 4465
llothar said:
I need a function that iterates over an array, like this one:

int iterate_array (void* mem, size_t size, callback func)
{
char* p = (char*)mem;
The cast is not required.
while(size-- 0) {
No, that won't do at all. It does not express your intent. See below:
func(p);
p += size;
}
}

but i'm not sure how to get the value of 'size'. First of course was
the idea

int a[10];
iterate_array(a, sizeof(int), intfunc)
Why not sizeof a[0] ?
but what if the data is 6 byte?
So what?
Then its up to the compiler (or pragmas) it might be aligned to 8.
No, because array elements are contiguous. Here, I express your intent
more clearly and correctly:

typedef void (*callback)(void *);

int iterate_array(void *arr, size_t nmemb, size_t size, callback func)
{
unsigned char *p = arr;
while(nmemb--)
{
func(p);
p += size;
}
return 0; /* presumably you had a reason for making this function
return int, so fix this up when you're ready. */
}

Usage:

int a[10] = {0};

iterate_array(a, sizeof a / sizeof a[0], sizeof a[0], intfunc);

Take a close look at qsort and bsearch. You may find it educational.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 23 '07 #2
No, that won't do at all. It does not express your intent. See below:

Sorry this was a stupid error, i had no code snippet so i typed it
here without thinking to much.
(I'm not such a bad programmer).
>
Then its up to the compiler (or pragmas) it might be aligned to 8.

No, because array elements are contiguous. Here, I express your intent
more clearly and correctly:
Really? Okay this would be an easy answer to the question.
And is this always true even in the different c compiler
implementations, regardless of any used pragma settings?

Apr 23 '07 #3
llothar said:

<snip>
And is [the contiguity of array members] always true even in the
different c compiler implementations, regardless of any used
pragma settings?
It is true for a conforming C implementation. It's entirely possible to
stop an implementation from being conforming by using pragmas. For
example, an implementation might supply support for a pragma whose
meaning is "treat all + as - and all - as +". There's no accounting for
pragmas.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 23 '07 #4
In article <11**********************@l77g2000hsb.googlegroups .com>,
llothar <ll*****@web.dewrote:
>No, because array elements are contiguous.
>Really? Okay this would be an easy answer to the question.
And is this always true even in the different c compiler
implementations, regardless of any used pragma settings?
It's true for all conforming compilers, but if you run them in a
non-conforming mode anything might be true. And pragmas can have any
effect.

I have used a computer that used 5-byte floats, but there wouldn't be
much point in that if they had to be padded to 8 bytes in arrays. And
even if for some reason it was desirable, it could be done by having
8-byte floats and only using 5 of the bytes in arithmetic operations.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Apr 23 '07 #5
Really? Okay this would be an easy answer to the question.
And is this always true even in the different c compiler
implementations, regardless of any used pragma settings?
Sorry to much bad home made alcohol yesterday.

I meant the difference between a size of a single item and an array
item. But with "sizeof a[0] " it should be clear to get this size (in
the caller not in the callee. )

I would guess there is an pragma, attribute that allows packing of the
items inside the array or not.

But i see that this is a case i can't catch anyway without having
access to the original array declaration.

Apr 23 '07 #6
On Apr 23, 9:14 pm, Richard Heathfield <r...@see.sig.invalidwrote:
iterate_array(a, sizeof a / sizeof a[0], sizeof a[0], intfunc);
what about when a is a pointer:
int b[10] = {0};
int *a = b;

"sizeof a" could get nothing but the size of that pointer.
Apr 24 '07 #7
et*******@gmail.com said:
On Apr 23, 9:14 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>iterate_array(a, sizeof a / sizeof a[0], sizeof a[0], intfunc);
what about when a is a pointer:
What about when a is an Edsel?

Check the subject line again. He asked about arrays, not pointers.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 24 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: KellyH | last post by:
Hi, I hope someone can point me in the right direction. I'll get it out of the way: Yes, I am a college student. No, I am not looking for anyone to do my homework, just looking for help. I have...
11
by: Dr John Stockton | last post by:
Q1 : Given an array such as might have been generated by var A = is there a highly effective way of reducing it to - i.e. removing the undefineds and shifting the rest down? ...
3
by: R. Rajesh Jeba Anbiah | last post by:
Unfortunately, I couldn't find any way to traverse the object array in reverse order. I'd thought there must be a way to do it with for..in loop, but couldn't find anything yet. Could someone...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
5
by: Manish | last post by:
This is the print_r() for a variable $categories. $categories :: Array ( =Array ( =Array (
2
by: rvimalram | last post by:
Hi all, I have a created a multidimesional array,what i need is to traverse that array and return the value of the array in the particular page the array is $one=array("one" =>"Text1");...
4
by: Praveen | last post by:
Hi, I would like to know how to traverse an array spirally? i.e if I have an array as follows 1 2 3 4 5 6 7 8 9 10 11 12 I want the output to be 1 2 3 4 8 12 11 10 9 5 6 7.
7
by: sumitshining | last post by:
if there r around 100000 elements in an array then how to count no of elements with exactly two repetitions in a single traverse????
4
by: raylopez99 | last post by:
I would like to know if there's a quick "Linq" way to find the index of an array having a particular value. I can do this the long way by sequential iteration, but would like to know if there's a...
6
by: Premal Mishra | last post by:
How to traverse a binary tree whose nodes only contain a father field. e.g. struct NodeType { int data; int father; }; A left son's father field has a negative value and right son's father...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.