By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,144 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,144 IT Pros & Developers. It's quick & easy.

ARRAYS AND FUNCTIONS

P: n/a
How to pass a full array into a function?

Dec 5 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
coinjo wrote:
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
Dec 5 '05 #2

P: n/a
coinjo wrote:
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.

Dec 5 '05 #3

P: n/a
coinjo wrote:
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.

Dec 5 '05 #4

P: n/a
Ok. Tell me how to pass a pointer to the first element of the array to
the function?

Dec 5 '05 #5

P: n/a
I can't avoid them cause i HAVE to use them!

Dec 5 '05 #6

P: n/a
coinjo wrote:
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
Dec 5 '05 #7

P: n/a
coinjo wrote:
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"?
Dec 5 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.