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

pass an array throuh a function

P: n/a
Hello,
can anyone tell me how to pass an array to a function ?
I have this function , part of my class.
It works if I do not put in int a[i] everywhere , but obviously , I need to
add an array so I can keep everything neat and tidy, And to call the array
whenever I want

thanks
kenny
void Setting::SetCylinder(int r, int h, int s, int a[i])
{
int value;
for (int i=0; i<count; i++)
{
cout<<" Enter a time: " <<endl;
cin>> value;
a[i].hours = value;

cout<<" Enter a minute: " <<endl;
cin>> value;
a[i].minutes = value;

cout<<" Enter a second: " <<endl;
cin>> value;
a[i].seconds = value;
}
}
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Tue, 25 May 2004 18:12:12 -0400, "Kenny" <le******@yah00.c0m> wrote:
[snip]

I've replied to your identical post on alt.comp.lang.learn.c-c++. Please do
not multi-post; if you /must/ post to multiple groups (and there's seldom
good reason to do so), at least "cross-post" by including all the groups in
your To: list at once.
Thanks,
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2

P: n/a
Kenny wrote:
Hello,
can anyone tell me how to pass an array to a function ?
I have this function , part of my class.
It works if I do not put in int a[i] everywhere , but obviously , I
need to add an array so I can keep everything neat and tidy, And to
call the array whenever I want


Your code seems to suggest that a should not be an array of int, for one
thing, so that's wrong.

You could use something like this:
-----
void Setting::SetCYlinder(int r, int h, int s, int a[], int count)
{
for (int i=0; i<count; i++)
{
/* do something */
}
}
-----
The disadvantage is that this can lead to hard to trace bugs if you pass a
'count' value that's not the actual length of the array.

The safe way is to use std::vector instead of arrays:
-----
void Setting::SetCYlinder(int r, int h, int s, std::vector<int> a)
{
for (int i=0; i<a.length(); i++)
{
/* do something */
}
}
-----

--
Unforgiven

Jul 22 '05 #3

P: n/a
Kenny wrote:
Hello,
can anyone tell me how to pass an array to a function ?
You can't. You can - however - pass a pointer to the array's first
element or you can pass a reference to an array.
I have this function , part of my class.
It works if I do not put in int a[i] everywhere , but obviously , I
need to add an array so I can keep everything neat and tidy, And to
call the array whenever I want

thanks
kenny
void Setting::SetCylinder(int r, int h, int s, int a[i])
{
int value;
for (int i=0; i<count; i++)
Where do you get 'count' from?
{
cout<<" Enter a time: " <<endl;
cin>> value;
a[i].hours = value;

cout<<" Enter a minute: " <<endl;
cin>> value;
a[i].minutes = value;

cout<<" Enter a second: " <<endl;
cin>> value;
a[i].seconds = value;
}
}

Either try:

void Setting::SetCylinder(int r, int h, int s, int* a)

This will pass a pointer to the first element of the array. The rest can
stay as it was. But you might need to add an additional parameter to
pass the size.
Alternatively, if the size is fixed at compile time:

void Setting::SetCylinder(int r, int h, int s, int (&a)[size])

with 'size' being the size of the array.

Jul 22 '05 #4

P: n/a
I wil try that thanks,

I also get a syntax error at this line:

a[i].SetCylinder(20,30,40, a[] );

: : error C2059: syntax error : ']'

Know what that means ?

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c9*************@news.t-online.com...
Kenny wrote:
Hello,
can anyone tell me how to pass an array to a function ?


You can't. You can - however - pass a pointer to the array's first
element or you can pass a reference to an array.
I have this function , part of my class.
It works if I do not put in int a[i] everywhere , but obviously , I
need to add an array so I can keep everything neat and tidy, And to
call the array whenever I want

thanks
kenny
void Setting::SetCylinder(int r, int h, int s, int a[i])
{
int value;
for (int i=0; i<count; i++)


Where do you get 'count' from?
{
cout<<" Enter a time: " <<endl;
cin>> value;
a[i].hours = value;

cout<<" Enter a minute: " <<endl;
cin>> value;
a[i].minutes = value;

cout<<" Enter a second: " <<endl;
cin>> value;
a[i].seconds = value;
}
}

Either try:

void Setting::SetCylinder(int r, int h, int s, int* a)

This will pass a pointer to the first element of the array. The rest can
stay as it was. But you might need to add an additional parameter to
pass the size.
Alternatively, if the size is fixed at compile time:

void Setting::SetCylinder(int r, int h, int s, int (&a)[size])

with 'size' being the size of the array.

Jul 22 '05 #5

P: n/a
Kenny wrote:
Know what that means ?

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c9*************@news.t-online.com...
Kenny wrote:

Hello,
can anyone tell me how to pass an array to a function ?


You can't. You can - however - pass a pointer to the array's first
element or you can pass a reference to an array.

I have this function , part of my class.
It works if I do not put in int a[i] everywhere , but obviously , I
need to add an array so I can keep everything neat and tidy, And to
call the array whenever I want

thanks
kenny
void Setting::SetCylinder(int r, int h, int s, int a[i])
{
int value;
for (int i=0; i<count; i++)


Where do you get 'count' from?

{
cout<<" Enter a time: " <<endl;
cin>> value;
a[i].hours = value;

cout<<" Enter a minute: " <<endl;
cin>> value;
a[i].minutes = value;

cout<<" Enter a second: " <<endl;
cin>> value;
a[i].seconds = value;
}
}

Either try:

void Setting::SetCylinder(int r, int h, int s, int* a)

This will pass a pointer to the first element of the array. The rest can
stay as it was. But you might need to add an additional parameter to
pass the size.
Alternatively, if the size is fixed at compile time:

void Setting::SetCylinder(int r, int h, int s, int (&a)[size])

with 'size' being the size of the array.


I wil try that thanks,

I also get a syntax error at this line:

a[i].SetCylinder(20,30,40, a[] );

: : error C2059: syntax error : ']'


Quick array/pointer lesson:

1. The name of an array, with no subscript, will serve a pointer to the
first element in the array.

2. a[i], when 'a' is a pointer, is shorthand for: *(a + i) ... the
significance of this is that, if you have a pointer to the first element
in an array, you can access the ith element with a[i].
So ...

void Setting::SetCylinder(int r, int h, int s, int* a, int count)
{
int i;
for (i = 0; i < count; i++)
a[i] = i; // or whatever you want to do.
}
To call this function:

Setting s;
int a[50];

s.SetCylinder(20, 30, 40, a, 50) ;

Alan
Jul 22 '05 #6

P: n/a

"Kenny" <le******@yah00.c0m> wrote in message
news:KK*****************@news20.bellglobal.com...
I wil try that thanks,

I also get a syntax error at this line:

a[i].SetCylinder(20,30,40, a[] );

: : error C2059: syntax error : ']'

Know what that means ?


It means a[] is illegal, why did you think it was ok? You use the name of
the array to stand for the array (but actually is is converted to a pointer
to the start of the array).

a[i].SetCylinder(20,30,40, a);

The subject of pointers and arrays is very confusing to a newbie, get a good
C++ book and read it carefully, remember its probably not saying what you
think its saying.

john
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.