Some Coder Guy
Some Coder Guy

Reputation: 33

How can i pass a structure to a function

I have called a function with seven parameters from main

find_sync(es_data + (loop_count * size) + chunk_bytes_counter, 
                size - chunk_bytes_counter, &sync_index, &flag, 
                &sync_length, &chunk_bytes_counter, &total_bytes_counter);

in function.c:

void find_sync(char data[], size_t size, unsigned int *sync_index, int *flag, unsigned int *sync_length, unsigned int *chunk_bytes_counter, unsigned int *total_bytes_counter)

prototype in header file:

extern void find_sync(char data[], size_t size, unsigned int *sync_index, int *flag, unsigned int *sync_length, unsigned int *bytes_counter, unsigned int *total_bytes_counter);

Now, my question is, how can i declare all these 7 parameters in a structure, so that i can only pass one structure variable.

Upvotes: 2

Views: 130

Answers (5)

rslemos
rslemos

Reputation: 2731

Begin by declaring the struct:

struct find_sync_parameters {
    char* data;
    size_t size;
    unsigned int *sync_index;
    int *flag;
    unsigned int *sync_length;
    unsigned int *bytes_counter;
    unsigned int *total_bytes_counter;
}

Then change your function signature either to:

void find_sync(struct find_sync_parameters param)

Or to

void find_sync(struct find_sync_parameters *param)

In the first case the whole struct will be pushed onto the stack before transferring control to find_sync. On the second case only a pointer to the struct (stored elsewhere) will be pushed.

There are advantages and drawbacks in each one. When passing a pointer note that the function can change the contents (this can be positive: for returning values directly inside the struct; also can be negative: the caller cannot be sure if its data were changed or not). If the struct is too big (not your case), then pushing everything onto the stack can take a significant amount of time and become a performance hit.

Inside the function you use it either with '.' (dot, the first case) or '->' (arrow, the second case) operator.

To call it:

struct find_sync_parameters p = { ... };
find_sync(p); // first case
find_sync(&p); // second case

If you find it annoying to type struct find_sync_parameters everytime you can define a new type with typedef:

typedef struct find_sync_parameters find_sync_parameters;

Or in one line (struct and typedef definitions):

typedef struct find_sync_parameters {
    ...
} find_sync_parameters;

Or even without struct name (anonymous struct)

typedef struct {
    ...
} find_sync_parameters;

In this last case you cannot reference the struct itself inside the struct (the case, for example, with linked list nodes).

Upvotes: 4

PU.
PU.

Reputation: 148

Here is a simple example demonstrating how to pass a structure to a function:

#include<stdio.h>
#include<conio.h>
//-------------------------------------
struct Example
{
  int num1;
  int num2;
}s[3];
//-------------------------------------
void accept(struct Example *sptr)
{
  printf("\nEnter num1 : ");
  scanf("%d",&sptr->num1);
  printf("\nEnter num2 : ");
  scanf("%d",&sptr->num2);
}
//-------------------------------------
void print(struct Example *sptr)
{
  printf("\nNum1 : %d",sptr->num1);
  printf("\nNum2 : %d",sptr->num2);
}
//-------------------------------------
void main()
{
int i;
clrscr();
for(i=0;i<3;i++)
accept(&s[i]);

for(i=0;i<3;i++)
print(&s[i]);

getch();
}

Upvotes: 0

R Sahu
R Sahu

Reputation: 206577

I am going to suggest:

struct find_sync_struct {
    char* data;
    size_t size;
    unsigned int sync_index;
    int flag;
    unsigned int sync_length;
    unsigned int bytes_counter;
    unsigned int total_bytes_counter;
};

Change the input argument of find_sync to:

void find_sync(struct find_sync_struct* strPtr);

Call the function using:

struct find_sync_struct str;
// Set str.data to something suitable.
// ....
find_sync(&str);

Upvotes: 0

Ruslan Gerasimov
Ruslan Gerasimov

Reputation: 1782

Just put in structure .

struct pars_t 
{
    char data[];
    size_t size; unsigned int *sync_index;
    int *flag; unsigned int *sync_length;
    unsigned int *bytes_counter;
    unsigned int *total_bytes_counter;
} pars;

and then call foo (pars_t par)

Upvotes: 0

Ricky Mutschlechner
Ricky Mutschlechner

Reputation: 4409

You can create a struct (and typedef it at the same time, to make it usable without saying "struct" every time) like so:

typedef struct _find_sync_str{
    char* data;
    size_t size;
    unsigned int *sync_index;
    int *flag;
    unsigned int *sync_length;
    unsigned int *bytes_counter;
    unsigned int *total_bytes_counter;
} find_sync_str

Then you can list the function as:

void find_sync(find_sync_str f);

Upvotes: 0

Related Questions