Hi there,
i've ran in a problem and made up an example code of my prob.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct mystruct
{
    int a;
}mytype;

mytype* createMytype(int x)
{
    mytype *mt;

    mt = (mytype*)calloc(1, sizeof(mytype));

    mt->a = x;

    return mt;
}

int foo(mytype* mt)
{
    mt = createMytype(6);
    return 0;
}

int bar(mytype* mt)
{
    printf("%p\n", mt->a);
    return 0;
}

int main(int argc, const char **argv)
{
    mytype *mt;

    mt = (mytype*)calloc(1, sizeof(mytype*));

    foo(mt);
    bar(mt);
    exit(0);
}

The problem is, after leaving the foo-scope the struct is set to null.
Can somebody explain my fault?
Thx in advance

Recommended Answers

All 7 Replies

Try doing it like below

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct mystruct
{
    int a;
}mytype;

mytype* createMytype(int x)
{
    mytype *mt;

    mt = (mytype*)calloc(1, sizeof(mytype));

    mt->a = x;

    return mt;
}

int foo(mytype** mt)
{
    *mt = createMytype(6);
    return 0;
}

int bar(mytype** mt)
{
    printf("%d\n", (*mt)->a);
    return 0;
}

int main(int argc, const char **argv)
{
    mytype *mt = NULL;

    foo(&mt);
    bar(&mt);
    exit(0);
}

This doesnt even compile-.-

This doesnt even compile-.-

That's odd, it compiles on my system without a warning or error. It also runs correctly.

I'm using

gcc (GCC) 4.4.3

Ok got it now. But why do i need to wrap another pointer around it?

mt = (mytype*)calloc(1, sizeof(mytype));

Better is

mt = calloc(1, sizeof *mt);

1) don't cast the result of malloc() (and friends), because all it does is hide a useful warning; and
2) sizeof *mt is shorter than sizeof(mytype) , and you don't have to change it if you later change the type of mt.

Other than that, yep, pretty much, just what gerard4143 said.

I have a personal vendetta against typedef, so I would eliminate that, too.

Ok got it now. But why do i need to wrap another pointer around it?

If you think about what happens when you call a function, it makes sense.

When you call your function the first thing it does is create temporary variables for the function arguments then it copies the values into to it...

So if we have a function like so

void myfunc(int * i);

and have a int pointer like so

int *iptr = NULL;

When we call myfunc(iptr) we'll create a temporary variable for the parameter i and copy the contents of iptr into it. Notice we copied NULL into the temporary and iptr still equals NULL.

Now lets look at my scenario where I define my function like so

void myfunc(int **i);

and we'll use the same pointer

int *iptr = NULL;

Now when I call the function I pass the address of iptr which is copied into the temporary(myfunc(&iptr)) so we can access the original pointer value with simple dereferencing.

Thx for pointing me into the right direction.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.