How can I pass an array as parameters to a vararg function? - c

I have some code that looks like this:
uint8_t activities[8];
uint8_t numActivities = 0;
...
activities[numActivities++] = someValue;
...
activities[numActivities++] = someOtherValue;
...
switch (numActivities)
{
0 : break;
1 : LogEvent(1, activities[0]); break;
2 : LogEvent(1, activities[0], activities[1]); break;
3 : LogEvent(1, activities[0], activities[1], activities[2]); break;
// and so on
}
where LogEvent() is a varargs function.
Is there a more elgant way to do this?
[Update] Aplogies to #0x69 et al. I omitted to say that there are many cases where LogEvent() could not take an array as a parameter. Sorry.

There's no standard way to construct or manipulate va_args arguments, or even pass them to another function (Standard way to manipulate variadic arguments?, C Programming: Forward variable argument list). You'd be better off seeing if you can access the internal routines of LogEvent.

pass a pointer to the array of ints and a number of ints instead
#include <stdio.h>
void logevent(int n, int num, int *l) {
int i;
for (i=0; i<num; i++) {
printf("%d %d\n",n,*(l++));
}
}
int main() {
int activities[8];
activities[0]=2;
activities[1]=3;
activities[2]=4;
int num=3;
int n=1;
logevent(n,num, activities);
printf("=========\n");
n=2;
activities[3]=5;
num=4;
logevent(n,num, activities);
}

Related

How can i use only one function showCnt() to show the members of struct arrays? How to pass parameters?

I'm learning a piece of code, I don't know how to implement this function using only one function? How to pass parameters to the function?
#include <stdio.h>
struct colleagues {
int female[10];
int male[10];
} teams[20];
void func(int count[])
{
for (int i = 0; i < 10; i++) {
printf("%d\n", count[i]);
}
}
//FIXME
void showCnt(void (*function)(int *), int XXX[])
{
for (int n = 0; n < 20; n++) {
func(teams[n].XXX);
}
}
int main()
{
//How can i just use only one function showCnt to do this work???, I'm so appreciated.
//***FIXME, How can i pass parameters "teams.female" to showCnt???***
showCnt(function, teams.female);
showCnt(function, teams.male);
return 0;
}
Sorry, I didn't say it clearly. I don't know how to pass the parameters "teams.male" as a array to showCnt(), I'm wondering how to call showCnt() twice separately to print the info.
For example, first, It calls showCnt(teamXX.female), second, it calls showCnt(teamXXX.male). I don't know if I can pass parameters to showCnt() to make "func(team[n].male)" and "func(team[n].female)" working properly.
If I understand you correctly, you want to only call showCnt as
showCnt(function);
and it should print both teams.female and teams.male?
Then to begin with remove the (erroneous) count argument from the function showCnt. Then inside the loop of showCnt call function twice with teams[n].female and teams[n].male.

How do I use the value of a variable in a function else where?

I have a certain program that lets you register members and save their name and birthdate into arrays. The particular function that does this registration uses the following code;
char regmember (struct member a[])
{
int i = 0;
char wow;
do
{
//registration
printf("\n Do you want to add someone else (y/n):");
scanf(" %c",&wow);
i++
}while(wow != 'n');
int nrofmembers = i;
return nrofmembers;
}
-> I save the user input by using
scanf("%s",a[i].name) and scanf("%d",&a[i].ID);
which is why I am using i++. As you realize, the int variable i, will hold the number of members who have been registered. I want to utilize this info in order to use it in loops in other functions, so I went on to save the value of i in another int variable...
int nrofmembers = i;
My problem is, I can't use that variable (nrofmembers) else where, even though I tried returning it, any advice?
you need both to get i in parameter and to return the new value, you can do
int regmember (struct member a[], int i)
{
... use and modify i
return i;
}
or using it as an input-output variable
void regmember (struct member a[], int * i)
{
... use and modify *i
}
In the first case the caller do for instance :
int i = 0;
for (...) {
...
i = regmember(..., i);
...
}
and in the second case :
int i = 0;
for (...) {
...
regmember(..., &i);
...
}
Suppose you keep the members in a global array, then you can manage how many members are in your array also as a global variable, for example
struct member gMembers[MAX_MEMBERS];
int gnMembers;
Your function can now operate on this array directly:
int regmember (void)
{
if (gnMembers < MAX_MEMBERS)
{
// add member
if (scanf("%s",gMembers[gnMembers].name)==1
&& scanf("%d",&gMembers[gnMembers].ID)==1) {
gnMembers++;
return 1; // success
}
}
return 0; // array full or scanf error
}

how to assign/initialize the array of function pointers only when i call a function to do so

#include <stdio.h>
#include <stdlib.h>
int (*(*ptr[2])[2])(); //arr of pointers to the array of func pointer
int (*arr_1[2])(int ,int);//arr of function pointers
(*arr_1[])()={add1, sub1};//add1 ,sub1 are simple functions returning int
int (*arr_2[2])(int ,int);
int a,b, user_func_choice;
(*arr_2[])()={add2, sub2};
int lib_choice,user_lib_choice;
int main(int argc , char* argv[]) {
printf("enter the lib number\t 1:Lib1 , 2:Lib2 \n");
scanf("%d", &user_lib_choice);
lib_choice=user_lib_choice-1;
if(lib_choice==0){
printf("Welcome to lib1\n");
printf("enter func choice==> 1: Add , 2: subtract\n");
scanf("%d", &user_func_choice);
printf("enter the numbers.\n");
scanf("%d%d",&a,&b);
int func_choice= user_func_choice-1;
ptr[0]= arr_1;
if(func_choice==0)
{
int sum1=(*(*ptr[lib_choice])[func_choice])(a,b);
printf("sum1=%1d\n\n", sum1);
}
else if(func_choice==1)
{
int subtract1=(*(*ptr[lib_choice])[func_choice])(a,b);
printf("sub1=%1d\n\n", subtract1);
}
else{printf("InValid Function/operator choice\n");}
return;
}
if(lib_choice=1){
int a, b, user_func_choice;
printf("welcome to lib2\n");
printf("enter func choice: 1: Add , 2: subtract\n");
scanf("%d", &user_func_choice);
printf("enter the numbers.\n");
scanf("%d%d",&a,&b);
ptr[1] = arr_2;
int func_choice= user_func_choice-1;
if(func_choice==0)
{
int sum2=(*(*ptr[lib_choice])[func_choice])(a,b);
printf("sum2=%1d\n\n", sum2);
}
else if(func_choice==1)
{
int subtract2=(*(*ptr[lib_choice])[func_choice])(a,b);
printf("sub2=%1d\n\n", subtract2);
}
return;
}
else{printf("InValid library choice\n");
}
return 0;
}
Every thing works fine here
But what i want is instead of hardcoding the initialization of library(lib1 or lib2) let user specify the number of libraries he want to intialize. Something like this
int main(int argc , char* argv[]){
printf("enter the lib number\t 1:Lib1 , 2:Lib2 \n");
scanf("%d", &user_lib_choice);
if(argc!=2){printf("please specify the library number as sencond argument\n");return 0;}
int lib_choice_cmd=atoi(argv[1]);
if(lib_choice==1)
{
lib1_init();
}
else if(lib_choice==2)
{
lib1_init();
lib2_init();
}
lib_init() is the function that will do the initialization process.If user specify lib num as 1 i will initialize only lib1. I did this for lib1, but this gives me an error
lib1_init(){
(*arr_1[2])(int ,int)={add1 , sub1};
}
And when i did
lib1_init(){
arr_1[0]=add1;
arr_1[1]=sub1;
}
This gives me a segmentation fault.Pleae tell me what is wrong
You can only do initialization when initializing, not later. Even given that, your syntax looks odd.
You can of course use plain assignment to each element:
void array_init(void)
{
arr_1[0] = add1;
arr_1[1] = sub1;
}
(*arr[2])()={add1 ,sub1};
Unless that line appears on the same line as the function pointer array initialization, it is complete nonsense code.
You can initialize the array upon declaration like this:
int(*arr[2])(int ,int) = {add1 ,sub1};
Or you can assign an individual item, just as for any array:
arr[0] = add1;
In C, you cannot assign arrays to arrays, nor can you assign several array items on a single line. That is, you can't do int arr[2]; arr = {1,2};Function pointer arrays are no different than other arrays.
If you want to initialize the array in run-time, you have to either set each item one by one in a loop, or use memcpy.
I did not right understood the problem, but there is a syntax mistake:
if you declare a pointer to function array like this:
int(*arr[2])(int ,int)
you have to call the function like this:
(*arr[0])(some_int_stuff, some_other_int_stuff);
the mistakes are that the index must be smaller than 2, and you don't pass any parameter

Sending values to another function using structures - Whats wrong with that?

I have two functions and a structure. My first function getChange does calculations and stores them in two arrays. My second function printChange should accept these values of two arrays and just print arrays using a simple for loop.
I am trying to use my structure Change to send these arrays across to the print function, but I can't seem to do it.
I thought of creating two arrays in my structure and then simply setting array in function = to array in struct and then do the opposite in print func. But I can't!
# include <stdio.h>
#include<stdlib.h>
struct cashback{
int value[8];
int money[8];
};
typedef struct cashback Change;
int getChange(int paid, int cost){
int r, k, cntr, c=1, value[8], money[8]={200,100,50,20,10,5,2,1};
Change store;
if (cost>paid){
printf("\t Insufficient Funds! \n");
getchar();
exit(1);
}
r=paid-cost;
value[0]=r/money[0];
k=r%money[0];
for(money[c], cntr=1;cntr<8;c++, cntr++){
if (k !=0){
value[c]= k/money[c];
k=k%money[c];
}
else{
value[c]=0;
k=k/money[c];
}
}
store.value[8]=value[8];
store.money[8]=money[8]; //Sending calculation to Struct(store)
}
void printChange() {
Change store;
int i, j;
for (i=0; i<8; i++) {
printf("%i \t",store.money[i] );
}
printf("\n \n");
for (j=0; j<8; j++) {
printf("%i \t ", store.value[j] );
}
}
int main(){
Change store;
getChange(90,50);
printChange();
getchar();
}
When you write:
Change store;
inside a function, that means there is a variable called store inside that function only. In your code you have three separate variables (all called store).
Instead you need to pass the variable around where it is needed.
You currently have getChange returning int but you never actually return a value. Instead you should make this return the change:
Change getChange(int paid, int cost)
{
Change change;
// ...
return change;
}
Similarly, the printChange function should be:
void printChange(Change change)
{
// ...
}
and your main function will look like:
Change store;
store = getChange(90, 50);
printChange(store);
OK, first of all inside the 'getChange', you don't use the 'Change' struct at all. Apart from using it in this declaration: Change a;
Also, there is certainly an error here: a.value =. With what is equal?
So, the money and value arrays that you declare inside 'getChange' have nothing to do with your struct.
And finally, it doesn't seem that printChange has anything to do with the rest of the program.
You can pass to both functions pointer to Change structure. The structure can be defined inside main:
int getChange(int paid, int cost, Change* a){
//...
a->value[c] = ...
//...
}
void printChange(Change* a) {
//...
printf("%i \t", a->value[i]);
//...
}
int main(){
Change a;
getChange(90,50, &a);
getchar(&a);
}

How to use two parameters pointing to the same structure in one function?

I have my code below that consits of a structure, a main, and a function. The function is supposed to display two parameters that have certain values, both of which point to the same structure.
The problem I dont know how to add the SECOND parameter onto the following code :
#include<stdio.h>
#define first 500
#define sec 500
struct trial{
int f;
int r;
float what[first][sec];
};
int trialtest(trial *test);
main(){
trial test;
trialtest(&test);
}
int trialtest(trial *test){
int z,x,i;
for(i=0;i<5;i++){
printf("%f,(*test).what[z][x]);
}
return 0;
}
I need to add a new parameter test_2 there (IN THE SAME FUNCTION) using this code :
for(i=0;i<5;i++){
printf("%f,(*test_2).what[z][x]);
How does int trialtest(trial *test) changes ?
and how does it change in main ?
I know that I should declare test_2 as well, like this :
trial test,test_2;
But what about passing the address in the function ? I do not need to edit it right ?
trialtest(&test); --- This will remain the same ?
So please, tell me how would I use test_2 as a parameter pointing to the same structure as test, both in the same function..
Thank you !!
Please tell me if you need more clarification
I think that this is your homework, so I'll just write a different function that may give you an idea of what (I think) you need to do. I read that you don't want to change the trail_test parameter list, so I stuck with a similar parameter list.
struct thing {
/* put some stuff here */
};
typedef struct thing thing; /* because this is C, not C++ */
int how_many_things(thing * thing_list);
int main(void) {
int i;
thing * a;
int count_init = random(); /* let's surprise ourselves and make a random number of these */
count_init %= 128; /* but not too many or it might not work at all */
a = malloc(count_init*sizeof(things)+1);
for (i = 0; i < count_init; i++) {
thing_init(&(a[i]));
}
make_illegal_thing(&(a[count_init]) ); /* like '\0' at the end of a string */
printf("There are %i things in the list\n", how_many_things(a) );
return 0;
}
/* This is very similar to strlen */
int how_many_things(thing * a) {
int count = 0;
while (is_legal_thing(a) ) {
a++;
count++;
}
return count;
}

Resources