Hi.
How do I write a function that returns an array of integers in C? Preferably I do not have to use pointers.
Pls reply.
Regards,
ec3_limz
array functions
Moderator: Board moderators
The only way to return an array is to return the pointer to the first element in the array. And you'll have to allocate this array within your function.
[c]int *returnInt()
{
return (int *)malloc(400 * sizeof(int));
}[/c]
This function returns int array of size 400. I guess I wrote the syntax correctly.
There's also a possiblilty to use static int array, but this array will contain it's values upto the next call to the function.
[c]int *returnInt()
{
static int a[400];
return a;
}[/c]
I guess this would also work.
About not using pointers. You can use pointer as a base -- indexing can still go through brackets.
Ivor
[c]int *returnInt()
{
return (int *)malloc(400 * sizeof(int));
}[/c]
This function returns int array of size 400. I guess I wrote the syntax correctly.
There's also a possiblilty to use static int array, but this array will contain it's values upto the next call to the function.
[c]int *returnInt()
{
static int a[400];
return a;
}[/c]
I guess this would also work.
About not using pointers. You can use pointer as a base -- indexing can still go through brackets.
Ivor
There is a theory which states that if ever anyone discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.
-
- New poster
- Posts: 27
- Joined: Sun Jul 07, 2002 6:46 pm
- Location: Campina Grande - Brazil
- Contact:
You're right. But are you aware how much time per each call will this kind of approach consume?
Ivor

Ivor
There is a theory which states that if ever anyone discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.
-
- New poster
- Posts: 27
- Joined: Sun Jul 07, 2002 6:46 pm
- Location: Campina Grande - Brazil
- Contact: