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::SetCyl inder(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;
}
} 6 1794
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.l earn.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
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::SetCYl inder(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::SetCYl inder(int r, int h, int s, std::vector<int > a)
{
for (int i=0; i<a.length(); i++)
{
/* do something */
}
}
-----
--
Unforgiven
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::SetCyl inder(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::SetCyl inder(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::SetCyl inder(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 : ']'
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::SetCyl inder(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::SetCyl inder(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::SetCyl inder(int r, int h, int s, int (&a)[size])
with 'size' being the size of the array.
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::SetCyl inder(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::SetCyl inder(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::SetCyl inder(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::SetCyl inder(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(2 0, 30, 40, a, 50) ;
Alan
"Kenny" <le******@yah00 .c0m> wrote in message
news:KK******** *********@news2 0.bellglobal.co m... 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Seeker |
last post by:
Newbie question here...
I have a form with some radio buttons. To verify that at least one of the
buttons was chosen I use the following code ("f" is my form object) :
var btnChosen;
for (count = 0; count <= 1; count++) {
if (eval(f.RadioButtons.checked)) {
btnChosen = true;
}
|
by: Te-Jung Lo |
last post by:
the following is the original code i wrote
#include <stdio.h>
#include <stdlib.h>
//--- Prototype ---//
void bfs(int );
void main()
|
by: Mark Dicken |
last post by:
Hi All
I have found the following Microsoft Technet 'Q'
Article :-
Q210368 -ACC2000: How to Pass an Array as an Argument to a Procedure
(I've also copied and pasted the whole contents into the bottom of
this email)
|
by: ritchie |
last post by:
Hi all,
I am new to this group and I have question that you may be able to
help me with.
I am trying to learn C but am currently stuck on this.
First of all, I have a function for each sort (Bubble, insertion,
selection..).
I have an array of int's and am passing them to each sort function.
|
by: wilson |
last post by:
Dear all,
In this time, I want to pass array to function.
What should I declare the parameter in the function?i
int array or int array? Which one is correct?
/********************************************************
Below is my code:
********************************************************/
| |
by: nospam |
last post by:
Hello!
I can pass a "pointer to a double" to a function that accepts
double*, like this:
int func(double* var) {
*var=1.0;
...
}
|
by: Abhi |
last post by:
I wrote a function foo(int arr) and its prototype
is declared as foo(int arr); I modify the values of the array in the
function and the values are getting modified in the main array which
is passed also. I understand that this way of passing the array is by
value and if the prototype is declared as foo(int *), it is by
reference in which case the value if modified in the function will get
reflected in the main function as well. I dont...
|
by: QQ |
last post by:
I have one integer array
int A;
I need to pass this array into a function and evaluate this array in
this function
how should I pass?
Is it fine?
void test(int *a)
|
by: venkatagmail |
last post by:
I have problem understanding pass by value and pass by reference and
want to how how they are or appear in the memory:
I had to get my basics right again. I create an array and try all
possible ways of passing an array. In the following code, fun1(int
a1) - same as fun1(int* a1) - where both are of the type passed by
reference. Inside this function, another pointer a1 is created whose
address &a1 is different from that of the passed...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |