# Thread: Arrays in Function Arguments

1. ## Arrays in Function Arguments

Hello,

I'm wondering if I can list contents in an array within a function argument?

For instance:

Code:
`RetreiveArrayInput({2, 1, 3, 0xFF});`
I have devised the following code to experiment with, but unfortunately I don't exactly know what I'm doing.

Code:
```#include <stdio.h>

//** Defines ************************************************************
#define		TERMINATOR		0xFF

//** Globals ************************************************************
unsigned int ArrayOfMyLife[] = {0x01, 0x02, 0x04, 0x08};

void RetreiveArrayInput(unsigned int *OrderOfValues)
{
unsigned int i;
unsigned int index;

while((index = *OrderOfValues++) != TERMINATOR)
{
for(i = ArrayOfMyLife[index]; i > 0; i--)
{
printf("Selection: %s\n", i);
}
}
}

int main()
{
unsigned int someArray[5];
RetreiveArrayInput({2, 1, 3, 0xFF});

system("PAUSE");
}```
Just in case, I'm using Dev-C++.

Thanks!

2. Compound Literal
In C99, you can use compound literal.
Code:
`RetreiveArrayInput(( unsigned int[]) { 2,1,3, TERMINATOR } ) ;`
Which is the same as in C89
Code:
```unsigned int __some_anonymous_array[] = { 2,1,3,TERMINATOR };
RetreiveArrayInput( __some_anonymous_array );```

3. Ok, that makes sense. I have another question as well. If I have the following code:
Code:
```char a[] = {“Hello”};
char *p;

p = a;		// Does p = “Hello” also?```
In other words, it points to the entire quantity of the array? Can I do this?
Code:
`printf("%s", p[0]);      // Should this equal 'H' ?`

4. Easy way to find out....

Code:
```char a[] = {“Hello”};   // These need to be char not int.
char *p;

p = a;		// Does p = “Hello” also?

puts(p);           // well, lets have a look...```

5. Aha, it does. But doing a p[0] didn't work. I expect I would have to do this:

Code:
`p = &a[0];     // This is 'H'`

6. Code:
`printf("%s", p[0]);      // Should this equal 'H' ?`
You want %c for an individual character. At this point, p points the the start of a, and refers to "Hello". You can index a pointer just like an array, so p[0] == a[0] == 'H', an individual character.

Code:
`p = &a[0]`
This is the same as p = a, since the name of an array serves as a pointer to it's first element.

7. Originally Posted by ElectroNerd
Aha, it does. But doing a p[0] didn't work. I expect I would have to do this:

Code:
`p = &a[0];     // This is 'H'`
Well, one way to find out.... TRY IT!

Really, it harms nothing to try little experiments like this...

8. Really? I imagined my computer blowing up!

I like the Alt-F4 Windows Aptitude Test; what is crazy is that I learned that function today at my work (some engineer did it, so I inquired how).

9. The OS will not let a simple program error blow it up. You may get seg-faults or access violation errors, or your program may crash in some very inglorious way... but except in extreme situations (most often as the result of deliberacy) one blown-out program will not crash a system.

IOW... what do you think error messages are for? That's your system protecting itself.