473,382 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

ARRAYS AND FUNCTIONS

How to pass a full array into a function?

Dec 5 '05 #1
7 1242
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
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
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
Ok. Tell me how to pass a pointer to the first element of the array to
the function?

Dec 5 '05 #5
I can't avoid them cause i HAVE to use them!

Dec 5 '05 #6
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: csx | last post by:
Hi everyone! two quick questions relating to arrays. Q1, Is it possible to re-assign array elements? int array = {{2,4}, {4,5}}; array = {2,3}
19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
29
weaknessforcats
by: weaknessforcats | last post by:
Arrays Revealed Introduction Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with arrays and array syntax but is not clear on a )exactly how...
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
0
by: datapata | last post by:
Hi I have some C++ code that I need to access from Python. I don't need to access any classes or anything, just a couple of functions that take one array as input and produce another array as...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.