Passing parameter to function

Thread Starter

Dadu@

Joined Feb 4, 2022
155
Hello forum!

So far I understood that we can pass the value of the variable, the location of the variable to the function in C language.

What do we mean when we say pass struct to function?

Does it mean that we can pass the value of struct variable. we can pass location of struct variable.
 

ZCochran98

Joined Jul 24, 2018
351
If I'm not mistaken, when we pass a struct to a function, by default we pass it by value. The elements of the struct are essentially squashed together in memory, kind of like how an array is. So, when it passes by value, it takes that chunk of memory, reads to the end, and passes that as one gigantic number. When it passes by reference, it grabs the memory location - like with regular variables.
The following C code gives a demonstration of this:
C:
#include <stdio.h>

typedef struct mystruct
{
    int a;
    long long b;
} mystruct;

void printstruct(mystruct s);
void printstruct_a(mystruct * s);

int main()
{
    //declaration of our structure
    mystruct here = {1,2};

    int l = sizeof(here);                        //get the size of our structure
    int incr = sizeof(long long)/sizeof(int);    //get the increment
                                                //a "word" size in memory is most likely going
                                                //to be the length of an integer, so we need
                                                //to properly increment by the correct number of
                                                //words to get the whole slice of memory.
                                                //Because structs, by default, use the widest memory
                                                //size of its fields for all fields, if we have an increment
                                                //of just 1 memory spot to the right,
                                                //then we'll be grabbing half of our memory data from the
                                                //left half of the struct and half of our memory data from
                                                //the right half, leading to incorrect results.
    unsigned int * hereptr = &here;                //get the memory address at which it is stored
    unsigned int leftside = *hereptr;            //get the left half of the memory block it occupies
    unsigned int rightside = *(hereptr+incr);    //get the right half of the memory block it occupies
    /*
     * Important note: this shows that the C compiler understands that structs are basically
     * lists of data, which means that you can access each part of a struct like you would
     * an array. This further demonstrates that the data is, as far as the code/compiler is
     * concerned, stored sequentially in memory. So, if you're clever enough, you can
     * represent an array as a struct, or a struct as an array. The difference is, however
     * arrays need to be the same data type with each spot being fixed-length. Structs do not.
     */
    //HOW IT PROBABLY LOOKS IN MEMORY:
    //INT+LEAD PAD (a)    LONGLONG (b)
    //- - - - - - - - | - - - - - - - -
    //&here-----------|                        <- &here is this memory section
    //        &here+1----------|            <- &here+1 is this memory section
    //                  |&here+2---------|    <- &here+2 is this memory section

    //print out the info about the struct itself, including how it's stored in memory
    printf("SIZEOF(INT)      : %d\n",sizeof(int));
    printf("SIZEOF(LONG LONG): %d\n",sizeof(long long));
    printf("SIZEOF(MYSTRUCT) : %d\n\n", l);
    printf("AS WE CAN SEE, IT USES THE LARGER MEMORY SPOT BY DEFAULT.\n");
    printf("YOU CAN FORCE IT TO USE THE PRECISE BIT WIDTH OF EACH FIELD, BUT THAT IS NOT DONE HERE.\n\n");
    printf("THE STRUCTURE DEFINITION:\n");
    printf("\tmystruct\n\t{\n\t\tint a = %d;\n\t\tlong long b = %d;\n\t};\nIS STORED AS:\n",here.a,here.b);
    printf("\tmystruct = 0x%08X%08X\n",leftside,rightside);
    printf("\n");

    //print out what the struct looks like in main()
    printf("IN MAIN:\n");
    printf("\tmystruct.a = %d\n",here.a);
    printf("\tmystruct.b = %lld\n",here.b);
    printf("\t&mystruct = 0x%08X\n",&here);
    printf("\n");

    //pass it by value and reference both to show the difference
    printstruct(here);
    printstruct_a(&here);

    return 0;
}

void printstruct(mystruct s)
{
    printf("IN PRINTSTRUCT:\n");
    printf("\tmystruct.a = %d\n",s.a);
    printf("\tmystruct.b = %lld\n",s.b);
    printf("\t&mystruct = 0x%08X\n",&s);
    printf("\n");
}

void printstruct_a(mystruct * s)
{
    printf("IN PRINTSTRUCT_A:\n");
    printf("\tmystruct.a = %d\n",s->a);
    printf("\tmystruct.b = %lld\n",s->b);
    printf("\t&mystruct = 0x%08X\n",s);
    printf("\n");
}
 

ApacheKid

Joined Jan 12, 2015
1,762
Hello forum!

So far I understood that we can pass the value of the variable, the location of the variable to the function in C language.

What do we mean when we say pass struct to function?

Does it mean that we can pass the value of struct variable. we can pass location of struct variable.
With pass by value or pass by ref, the called function always "sees" a pointer to the struct (that is a pointer is always used as part of the caller-callee stack frame). In the case of pass by value that pointer points to a copy of the structure, made by copying the struct that the caller passes into the function call. That copy is created on the stack and so passing structs by value will consume more stack space than pass by ref as well as taking CPU time to perform the copying.

Of course, pass by ref simply passes a pointer to the struct in the caller, no copy is made.
 

djsfantasi

Joined Apr 11, 2010
9,237
A struct variable is always a pointer to a memory location. I am in total agreement with @ApacheKid as to the memory and impact of passing a struct type by value versus reference.

In case you’re wondering, the size of that memory block is processor/ compiler dependent. Some will compress store BYTES into WORDS (or INTegers). Others will waste 8 bits when storing a BYTE. Some will be high-endian while others will be low-endian. In normal use, this will be irrelevant. In high performance, memory constrained and/or high use of structs, this knowledge may be useful.

Since you may just be learning this concept, you might not need to consider this.
 
Last edited:

Thread Starter

Dadu@

Joined Feb 4, 2022
155
Am I passing struct in code ?

C:
#include <stdio.h>

struct mystruct
{
    int a;
};

void modified ( struct mystruct Var)
{
   Var.a = 0;
   printf(" a : %d \n",  Var.a);         
    
}

int main ()
{
    struct mystruct var;
  
    var.a = 1;
    printf(" a : %d \n",  var.a);     
    modified (var);  // Am I passing struct in this line ? I think yes
    printf(" a : %d \n",  var.a);     
    return 0;
}
a : 1
a : 0
a : 1

It seems to me that i am passing value of structure variable
 

ApacheKid

Joined Jan 12, 2015
1,762
Am I passing struct in code ?

C:
#include <stdio.h>

struct mystruct
{
    int a;
};

void modified ( struct mystruct Var)
{
   Var.a = 0;
   printf(" a : %d \n",  Var.a);        
   
}

int main ()
{
    struct mystruct var;
 
    var.a = 1;
    printf(" a : %d \n",  var.a);    
    modified (var);  // Am I passing struct in this line ? I think yes
    printf(" a : %d \n",  var.a);    
    return 0;
}
a : 1
a : 0
a : 1

It seems to me that i am passing value of structure variable
Yes you are, the called function "sees" a copy of the struct, any changes that the called function makes to the members of the struct are only made to the copy. Once the function returns the original caller will not see anything changed, the values of all the struct members will always be the same after the call as the were before the call.
 
Top