473,408 Members | 2,888 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,408 software developers and data experts.

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 2464
In article <11*********************@l77g2000hsb.googlegroups. com>,
QQ <ju****@yahoo.comwrote:
>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.comwrote in message
news:11*********************@l77g2000hsb.googlegro ups.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.comwrote:
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 "replacement": 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
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...
2
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...
0
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...
5
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? ...
9
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...
4
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: ...
5
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...
14
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...
6
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. ...
12
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....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
Oralloy
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,...
0
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...
0
agi2029
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,...
0
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...

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.