Manipulate structures in void function

I'm not very sure about using structs and am currently trying to pass a struct via a void function. The structure is defined in the header of the shards and so is my void function:

/* "main.h" */

struct input{
    unsigned int NO;
    double RA;
    double DE;
    double MV;
};

void full_view_equ(struct input, struct input);

      

The function looks like this: you don't need to use two different structures. Struct EQU already contains values, OUTPUT will be uninitialized:

/* "transformations.c" */

#include "main.h"

void full_view_equ(struct input EQU, struct input OUTPUT){
    OUTPUT.NO = EQU.NO;
    OUTPUT.RA = -radian2degree(EQU.RA);
    OUTPUT.DE = radian2degree(EQU.DE);
    OUTPUT.MV = EQU.MV;
}

      

I am calling a function with two structures EQU and OUTPUT like this:

struct input EQU, OUTPUT;

full_view_equ(EQU, OUTPUT);

      

The problem is that inside the function, OUTPUT has the expected values. Outside of a function, all OUTPUT records are zero.

I can't figure out what's wrong with it before I used arrays instead of structs and everything works fine.

+3


source to share


6 answers


You have to use a pointer to the structure and pass it to the function, in your code the OUTPUT structure in the function is actually a copy of the original, and changing the copy will not change the original. to declare a pointer to a structure, just use this code:

struct input *OUTPUT;

      

and use this header for your function:



void full_view_equ(struct input EQU, struct *input OUTPUT)

      

and the function call would be:

full_view_equ(EQU, &OUTPUT);

      

+3


source


Function arguments are passed by value. Therefore, you need to use pointers to change the value outside of the function:

void full_view_equ(struct input EQU, struct input *OUTPUT){
    OUTPUT->NO = EQU.NO;
    OUTPUT->RA = -radian2degree(EQU.RA);
    OUTPUT->DE = radian2degree(EQU.DE);
    OUTPUT->MV = EQU.MV;
}

      



and call it like

full_view_equ(EQU, &OUTPUT);

      

+1


source


If you declare a function with a struct parameter, you will send a copy of your structure to it. As soon as the function returns, this copy will be lost.

You can either modify your function to return the structure, in which case you need to copy it. Alternatively, you can modify your function to make a pointer to structures.

0


source


When you pass an array to a function, it decays to pointers in the function. Since the array name is the base address. But there are no structures. So pass a pointer to the structure!

Instead of this -

full_view_equ(EQU, OUTPUT);

      

use -

full_view_equ(EQU, &OUTPUT); // pass a pointer to a structure

      

and catch it by a structure pointer in a function, replace it .

with ->

operator

void full_view_equ(struct input EQU, struct input *OUTPUT){
OUTPUT->NO = EQU.NO;
OUTPUT->RA = -radian2degree(EQU.RA);
OUTPUT->DE = radian2degree(EQU.DE);
OUTPUT->MV = EQU.MV;
}

      

0


source


When you pass a variable to a function, a copy of that variable is made.

Here you need to pass a pointer to your struct and then use

my_struct->data

      

instead

my_struct.data

      

Then you will pass a copy of the function pointer, but the specified address will be your original structure, so you can change it :)

0


source


Either pass a pointer to the structure ...

void full_view_equ(struct input *EQU, struct input *OUTPUT){
    OUTPUT->NO = EQU->NO;
    OUTPUT->RA = -radian2degree(EQU->RA);
    OUTPUT->DE = radian2degree(EQU->DE);
    OUTPUT->MV = EQU->MV;
}

int main(void) {
    struct input a, b;
    memset(&b, 0, sizeof(b));
    full_view_equ(&a, &b)
}

      

Or return the structure from a function:

struct input full_view_equ(struct input EQU){
    struct input OUTPUT;
    OUTPUT.NO = EQU.NO;
    OUTPUT.RA = -radian2degree(EQU.RA);
    OUTPUT.DE = radian2degree(EQU.DE);
    OUTPUT.MV = EQU.MV;
    return OUTPUT;
}

int main(void) {
    struct input a, b;
    memset(&b, 0, sizeof(b));
    a = full_view_equ(b);
}

      

In theory, a copy of the structure should be made in the second case, but the compiler can optimize it.

0


source







All Articles