ARRAYS AND FUNCTIONS

V

Victor Bazarov

coinjo said:
How to pass a full array into a function?

What do you mean? Idiomatic approach is to pass a pointer to the first
element of the array to the function. Does it not work for you? Read
the FAQ 5.8. Arrays do not have copy semantics defined for them alone,
only if they are part of a struct. You can wrap your array in a struct
and pass that struct by value. But my question would be, 'why?'

V
 
N

Neelesh Bodas

coinjo said:
How to pass a full array into a function?

array name gets converted to a pointer to the first element when passed
to a function. You can very well write

int f(int a[])
{
// your code here
}

But this is just a syntactic sugar for
int f(int *a)
Arrays cannot be passed by value.

Also, avoid arrays. Use vector.
 
C

coinjo

Ok. Tell me how to pass a pointer to the first element of the array to
the function?
 
R

Rolf Magnus

coinjo said:
How to pass a full array into a function?

You can't directly pass an array to a function. You can pass a pointer to
its first element, or a reference to the array, or - if you want it to be
copied - a struct that contains the array as member.
Alternatively, simply use std::vector.
 
V

Victor Bazarov

coinjo said:
Ok. Tell me how to pass a pointer to the first element of the array to
the function?

"Tell me how to pass a pointer to the first element of the array to
the function, PLEASE".

void foo(int *ptr)
{
}

int main()
{
int array[100];
foo(array); // that's how
}

V
 
V

Victor Bazarov

coinjo said:
I can't avoid them cause i HAVE to use them!

"Avoid them"? Whom? Functions or arrays? Why are you
responding to _your_own_ post, in which you didn't even
use the word "avoid"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top