user18429: I know its probably an odd question...but I found a workaround for a foreach loop in C. I have a whole bunch of int arrays that I would like to be able to pass into a void, which has that foreach loop in it. This is my code right now:
int example[] = {1, 2, 3, 4, 5};

void setup () {
    Serial.begin(57600);

    #define foreach(item, array) \
        for(int keep = 1, \
            count = 0,\
            size = sizeof (array) / sizeof *(array); \
            keep && count != size; \
            keep = !keep, count++) \
        for(item = (array) + count; keep; keep = !keep)
    }
}

void Words (int w[]) {
    foreach(int *v, w) {
        Serial.println(*v);
        delay(500);
    }
}

void loop () {
    Words (example);
}

However when I run it, it only returns the first number of the array, I need all 5 of them. I know theres all sorts of additional things that could be it, like the * or & symbol but I really don't understand what they do. Any ideas as to how to get that to work?

user84: What you want will not work the way you think: in C++ (just like in C), arrays and pointers are almost the same, and the compiler won't be able to make a distinction between
void f(int* array)

and
void f(int array[])

In particular, inside f() the compiler won't be able to know the actual size of array: for the compiler sizeof array will always be the same as sizeof(int*), i.e. just the size of a pointer, always 2 bytes on Arduino UNO, whatever type it points to.
If you want your foreach macro to work correctly, you have to pass it the actual array count of items as an additional argument:
#define foreach(item, array, size) \
    for(int keep = 1, \
        count = 0;\
        keep && count != size; \
        keep = !keep, count++) \
    for(item = (array) + count; keep; keep = !keep)

And also pass the count of items in Words:
void Words(int w[], size_t size) {
    foreach(int *v, w, size) {
        Serial.println(*v);
        delay(500);
    }
}

In loop() you can use sizeof example, because the compiler knows what its size is, from its initialization (an array of 5 integers, i.e. 10 bytes):
void loop() {
    Words(example, sizeof example / sizeof example[0]);
}

You could even define a macro to compute the count of items in an array:
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))

It will work as long as you use it with an argument that is completely known by the compiler:
void loop() {
    Words(example, ARRAY_SIZE(example));
}

Important comments on your original code
You mentioned that you want to "save space", but defining a macro will not help in this regard, as a macro is NOT a function, hence its binary code will be repeated everywhere it is used. It can just help make your source code shorter and possibly clearer.
The way you defined your macro seems weird and overly complex to me: why use 2 for loops when you need only one?
It would have been possible to define it in a simpler way, for example:
#define foreach(type, var, array, size) \
    for(type* var = array; var != array + size; var++)

It would be used as follows:
void Words(int w[], size_t size) {
    foreach(int, v, w, size) {
        Serial.println(*v);
        delay(500);
    }
}

Using C++ templates may probably help too, but this is an advanced topic. I'd recommend reading first some good book about C++ before digging into templates.

18429: <p>I know its probably an odd question...but I found a workaround for a foreach loop in C. I have a whole bunch of int arrays that I would like to be able to pass into a void, which has that foreach loop in it. This is my code right now:</p>

<pre><code>int example[] = {1, 2, 3, 4, 5};

void setup () {
    Serial.begin(57600);

    #define foreach(item, array) \
        for(int keep = 1, \
            count = 0,\
            size = sizeof (array) / sizeof *(array); \
            keep &amp;&amp; count != size; \
            keep = !keep, count++) \
        for(item = (array) + count; keep; keep = !keep)
    }
}

void Words (int w[]) {
    foreach(int *v, w) {
        Serial.println(*v);
        delay(500);
    }
}

void loop () {
    Words (example);
}
</code></pre>

<p>However when I run it, it only returns the first number of the array, I need all 5 of them. I know theres all sorts of additional things that could be it, like the * or &amp; symbol but I really don't understand what they do. Any ideas as to how to get that to work?</p>
 84: <p>What you want will not work the way you think: in C++ (just like in C), arrays and pointers are almost the same, and the compiler won't be able to make a distinction between</p>

<pre><code>void f(int* array)
</code></pre>

<p>and</p>

<pre><code>void f(int array[])
</code></pre>

<p>In particular, inside <code>f()</code> the compiler won't be able to know the actual size of <code>array</code>: for the compiler <code>sizeof array</code> will always be the same as <code>sizeof(int*)</code>, i.e. just the size of a pointer, always 2 bytes on Arduino UNO, whatever type it points to.</p>

<p>If you want your <code>foreach</code> macro to work correctly, you have to pass it the actual array count of items as an additional argument:</p>

<pre><code>#define foreach(item, array, size) \
    for(int keep = 1, \
        count = 0;\
        keep &amp;&amp; count != size; \
        keep = !keep, count++) \
    for(item = (array) + count; keep; keep = !keep)
</code></pre>

<p>And also pass the count of items in <code>Words</code>:</p>

<pre><code>void Words(int w[], size_t size) {
    foreach(int *v, w, size) {
        Serial.println(*v);
        delay(500);
    }
}
</code></pre>

<p>In <code>loop()</code> you can use <code>sizeof example</code>, because the compiler knows what its size is, from its initialization (an array of 5 integers, i.e. 10 bytes):</p>

<pre><code>void loop() {
    Words(example, sizeof example / sizeof example[0]);
}
</code></pre>

<p>You could even define a macro to compute the count of items in an array:</p>

<pre><code>#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
</code></pre>

<p>It will work <strong>as long as you use it with an argument that is completely known by the compiler</strong>:</p>

<pre><code>void loop() {
    Words(example, ARRAY_SIZE(example));
}
</code></pre>

<h1>Important comments on your original code</h1>

<p>You mentioned that you want to "<em>save space</em>", but defining a macro will not help in this regard, as a macro is <strong>NOT</strong> a function, hence <strong>its <em>binary code</em> will be repeated everywhere it is used</strong>. It can just help make your <em>source code</em> shorter and possibly clearer.</p>

<p>The way you defined your macro seems weird and overly complex to me: why use 2 <code>for</code> loops when you need only one?
It would have been possible to define it in a simpler way, for example:</p>

<pre><code>#define foreach(type, var, array, size) \
    for(type* var = array; var != array + size; var++)
</code></pre>

<p>It would be used as follows:</p>

<pre><code>void Words(int w[], size_t size) {
    foreach(int, v, w, size) {
        Serial.println(*v);
        delay(500);
    }
}
</code></pre>

<p>Using C++ templates may probably help too, but this is an advanced topic. I'd recommend reading first some good book about C++ before digging into templates.</p>
