-1

I have this question in my mind since few weeks and I'm unable to find an answer for it.

Let say that we have this code:

typedef struct s_data
{
    int age;
    char *name;
} t_data;

int main()
{
    t_data someone;

    if ((foo(&someone)) != 0)
        printf("blablabla");

    //....

    return (0);
}

int foo(t_data *someone)
{
    // Get data from internal memory by example

    *someone = unjson("data.json");

    if (someone == null)
        return (-1);

    return (0);
}

So now, back in the parent, the object, the structure is updated, but how does it works if it's not a structure with a pointer, but an object? Like in C#

I pass my object, define that someone = unjson("....");, the object is update in the child, not in the parent. It makes sense to me and it's logic, but how can you make it works as the example I gave in C for a language as C#?

Maybe this question is stupid, maybe I didn't searched with the right key-words en google, but I really wanna know how can I do it.

Thank for any explanation !

Emixam23
  • 3,854
  • 8
  • 50
  • 107
  • Probably you are looking for the `ref` keyword in C#? Will pass your someone by reference (so basically will pass a pointer to it, just like in your C example). – Evk Mar 26 '17 at 21:29
  • Use `out` keyword instead. – Kalten Mar 26 '17 at 21:34

1 Answers1

1

add a ref keyword before the parameter in the method, this will send the object by reference same as your code.

     private int foo(ref myObjectType someone)    
{
    // Get data from internal memory by example

    someone = unjson("data.json");

    if (someone == null)
        return (-1);

    return (0);
}

more info:

https://msdn.microsoft.com/en-us/library/14akc2c7.aspx

YazX
  • 444
  • 4
  • 12