473,657 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to pass this parameter into the function

QQ
I have one integer array

int A[32];

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)
{
a[0]=0;
....
}
and call this function as

test(a)

thanks a lot

May 9 '07 #1
3 2495
In article <11************ *********@l77g2 000hsb.googlegr oups.com>,
QQ <ju****@yahoo.c omwrote:
>I have one integer array
>int A[32];
>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)
{
a[0]=0;
...
}
and call this function as
>test(a)
Close, but outside of test(), the variable 'a' declared in test()
does not exist, and you haven't done anything with your variable 'A'
that does exist.

You probably want to call

test(A);

--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
May 9 '07 #2

"QQ" <ju****@yahoo.c omwrote in message
news:11******** *************@l 77g2000hsb.goog legroups.com...
>I have one integer array

int A[32];

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)
{
a[0]=0;
...
}
and call this function as

test(a)

thanks a lot
#include <stdio.h>
void foo(int *ptr, int N);

int main(void)
{
int i;
int array[10] = {1,2,3,4,5,6,7, 8,9,10};
foo(array, 10);
for(i=0;i<10;i+ +)
printf("%d\n", array[i]);
}

void foo(int *ptr, int N)
{
int i;

for(i=0;i<N;i++ )
ptr[i] += 2;
}
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

May 9 '07 #3
QQ <ju****@yahoo.c omwrote:
I have one integer array
int A[32];
I need to pass this array into a function and evaluate this array in
this function
how should I pass?
You can't pass an array to a function in C. Variables are passed
always by value, i.e. a copy is made of watever you pass to a
function and that's what the function receives. But this does
not work with arrays - passing an array by value would require
that a copy of the whole array is made, and the inventors of C
decided that this would be too much trouble. But since "passing
an array" to a function is such a useful thing to do, they came
up with the following "replacemen t": when you try to pass an
array to a function, the address of the first element of the
array is passed to the function instead of the array itself.
Is it fine?
void test(int *a)
{
a[0]=0;
...
}
and call this function as
test(a)
I guess that should be

test( A );

since the array you defined above is called 'A' and not 'a'. And,
yes, basically it's ok since this will pass a pointer to the first
element of 'a' to the function, which has type "pointer to integer",
i.e. what the function expects. But there's a caveat: since the
function receives only a pointer to the first element it has no
knowledge about how long the array is. And without that informa-
tion you might accidentally try to access elements of the array
that are beyond its end. So to make the function safe in this re-
spect you also should pass the length of the array to the function
(of course unless you will never pass an array with less than 32
elements to the function and you thus can use that knowledge when
writing it).
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\______________ ____________ http://toerring.de
May 9 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

110
9899
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object must be an object instead of
2
2434
by: Yarik | last post by:
Hello there! I am working with MS SQL Server 2000. I have a table function that takes an integer parameter and returns a table, and I can successfully use it like this (passing a literal as a parameter): SELECT * FROM MyTableFunction(1)
0
3316
by: Zlatko Matiæ | last post by:
Hi everybody! Recently I was struggling with client/server issues in MS Access/PostgreSQL combination. Although Access is intuitive and easy to use desktop database solution, many problems appear when someone is trying to use it as front-end for real server database systems such as PostgreSQL or MySQL. One of these problems is regarding pass-through queries and parameters. I wanted to have all the code on client, while executing it on...
5
3414
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: ********************************************************/
9
2311
by: Alan Silver | last post by:
Hello, I'm a bit surprised at the amount of boilerplate code required to do standard data access in .NET and was looking for a way to improve matters. In Classic ASP, I used to have a common function that was included in all pages that took an SQL query and returned a disconnected recordset. This meant that data access could be achieved in a single line. I would like to do something similar in ASP.NET. I know I could just duplicate...
4
150719
by: _Mario.lat | last post by:
Hallo, I have a little question: In the function session_set_save_handler I can pass the name of function which deal with session. In Xoops code I see the use of this function like that: session_set_save_handler(array(&$sess_handler, 'open'), array(&$sess_handler, 'close'), array(&$sess_handler, 'read'), array(&$sess_handler, 'write'), array(&$sess_handler, 'destroy'), array(&$sess_handler, 'gc'));
5
7816
by: David++ | last post by:
Hi folks, I would be interested to hear peoples views on whether or not 'pass by reference' is allowed when using a Web Service method. The thing that troubles me about pass-by-reference into a WebService is that essentially we are passing an address of an object which resides on the 'local machine' i.e. a local machine object address. Surely when the WebService method is called and run 'on the server', the reference type will be...
14
20387
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...
6
2840
by: =?Utf-8?B?emlubw==?= | last post by:
I'm trying to pass a delegate as parameter but the code below does not compile. It display an error: 'AddressOf operand must the name of a method(without parantheses)' How can I make it work. Public Class CacheFactory Delegate Function myDelegateReport(ByVal myInfoStore As CrystalDecisions.Enterprise.InfoStore) As System.Collections.Generic.Dictionary(Of Long, String)
12
11073
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
0
8385
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8723
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8502
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,...
0
8602
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
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...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
2
1941
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
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...

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.