pass an array throuh a function

K

Kenny

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 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)
{
int value;
for (int i=0; i<count; i++)
{
cout<<" Enter a time: " <<endl;
cin>> value;
a.hours = value;

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

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

Leor Zolman

[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
 
U

Unforgiven

Kenny said:
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 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 */
}
}
 
R

Rolf Magnus

Kenny said:
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 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)
{
int value;
for (int i=0; i<count; i++)


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

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

cout<<" Enter a second: " <<endl;
cin>> value;
a.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.
 
K

Kenny

I wil try that thanks,

I also get a syntax error at this line:

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

: : error C2059: syntax error : ']'

Know what that means ?

Rolf Magnus said:
Kenny said:
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 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)
{
int value;
for (int i=0; i<count; i++)


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

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

cout<<" Enter a second: " <<endl;
cin>> value;
a.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.
 
A

Alan Johnson

Kenny said:
Know what that means ?

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 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)
{
int value;
for (int i=0; i<count; i++)


Where do you get 'count' from?

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

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

cout<<" Enter a second: " <<endl;
cin>> value;
a.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.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, 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.


So ...

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


To call this function:

Setting s;
int a[50];

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



Alan
 
J

John Harrison

Kenny said:
I wil try that thanks,

I also get a syntax error at this line:

a.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.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
 

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

Members online

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top