Search code examples
cscopesdlpass-by-referencesdl-2

How to pass local structures to functions?


The following link says that structs defined in main don't have the scope to be called by functions because they are local so you should define your structs globally. However with variables, it's preferred to declare variables locally and pass to functions with pointers instead of declaring global variables.

Is there a way in pure C using pointers etc to pass a struct defined in main to a function? If you don't mind, please use the example program to demonstrate the method. Thanks.

where to declare structures, inside main() or outside main()?

This code works but is not what I want. I want to define the structure within main. Is this possible?

#include <stdio.h>
#include <SDL2/SDL.h>

void function();

struct hexColour
    {
        Uint32 red;
    }hc;

int main(void)
{
    hc.red = 0xFFFF0000;
    function(hc);
    return 0;
}

void function(struct hexColour hc)
{
    printf("red is %x\n", hc.red);
}

What I want is:

int main(void)
{
    struct hexColour
    {
        Uint32 red;
    }hc;
    hc.red = 0xFFFF0000;
    function(hc);
    return 0;
}

Solution

  • First of all you should really use proper prototypes that matched the function definitions.

    Secondly, your example do pass a structure into the local variable hc in the function.

    When function is running there are two distinct and separate structures in memory: The one in the main function, and the local in the function function.


    To cover my bases, here are two answers for two other question that maybe is asked:

    1. You want to define the structure itself inside the main function, and then be able to use it in other functions.

      Something like

      int main(void)
      {
          struct hexColor
          {
              uint32_t white;
              // Other members omitted
          };
      
          struct hexColour hc;
          hc.white = 0xff;
      
          func(hc);  // Assume declaration exist
      }
      
      void func(struct hexColour my_colour)
      {
          printf("White is %u\n", my_colour.white);
      }
      

      This is not possible. The structure hexColour is defined inside the main function only. No other function can use that structure. It doesn't matter if you pass a pointer or not, the structure hexColour still will only exist inside the main function only.

    2. Emulate pass-by-reference by passing a pointer to a structure object. Like

      struct hexColor
      {
          uint32_t white;
          // Other members omitted
      };
      
      int main(void)
      {
          struct hexColour hc;
          hc.white = 0xff;
      
          // Assume declaration of function exists
          func(&hc);  // Emulate pass-by-reference by passing a pointer to a variable
      }
      
      void func(struct hexColour *colourPointer)
      {
          colourPointer->white = 0x00;
      }
      

      This is possible, because then the structure hexColour exists outside the main function, in the global scope. All functions declared and defined after the structure definition may use the structure and its members.