Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Can you define a function in C that can have indefinite parameters as inputs?

Status
Not open for further replies.

soniya_ahuja

Newbie level 4
Joined
Feb 11, 2006
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Mumbai
Activity points
1,348
Queries in C


Can anyone please tell me what are the advantages and disadvantages of macros? I would also lie to know the main difference between malloc and calloc.

One more question -

Can you define a function that can have indefinite parameters as inputs?

Thank you
Regards
Soniya
 

irata

Junior Member level 3
Joined
Dec 31, 2005
Messages
28
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,281
Activity points
1,499
c querries

Now i can answer you the last question.
We can define a function that have indefinite parameters as input.
for this problem we can use the stdarg macros which defined in the stdarg.h head file. this head file has defined a type va_list and 3 macros --va_start, va_arg , va_end. we can declare a varibale of va_list type, and with the three macros to access the parameters.

/*
** Compute the average of the specified number of values.
*/

#include <stdarg.h>

float
average( int n_values, ... )
{
va_list var_arg;
int count;
float sum = 0;

/*
** Prepare to access the variable arguments.
*/
va_start( var_arg, n_values );

/*
** Add the values from the variable argument list.
*/
for( count = 0; count < n_values; count += 1 ){
sum += va_arg( var_arg, int );
}

/*
** Done processing variable arguments.
*/
va_end( var_arg );

return sum / n_values;
}



all of this you can refer to <pointers on c>by kenneth A.Reek chapter 7.6 .

(forgive my poor english)
 

imperial_amazon

Newbie level 3
Joined
Feb 18, 2006
Messages
3
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Activity points
1,368
Re: C queries

malloc allocates a block of size specified from the memory. A program is clearly allowed to allocate memory as what is needed by it.

On success malloc returns a pointer to the newly allocated memory block

Calloc is similar to malloc rather it takes one more argument n_items which is multiplied with the second argument size_t which is the size to allocate and allocates a block of size n_items*size.

It also initializes all the locations to 0 which is not done by malloc.

Note: calloc can be used for allocating size upto 64K, for allocation beyond 64K, you've to use farcalloc.
 
Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top