0

Here's a very basic example of what I'm trying to do (note that this segmentation faults)

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

typedef struct foo {
    int *bar;
} Foo;

Foo **fooPointers() {
    Foo **test = (Foo**) malloc(sizeof(struct foo) * 3);
    for(int i = 0; i < 3; i++) {
        Foo *curr = *(test + i);
        int *num = &i;
        curr->bar = num;
    }
    return test;
}

int main() {
    fooPointers();
    return 0;
}

the goal is to create an array of pointers of Foo, give each element meaningful values, and then return the pointer array.

Is anyone able to point me in the right direction as to why this doesn't work and how I can accomplish this task?

6
  • 1
    malloc the wrong size for a start. If you want to malloc space for 3 pointers, you used the wrong size. Or if you want to malloc space for 3 struct foos, you used the wrong type for the result. Commented Nov 23, 2020 at 4:27
  • assuming the goal is in fact "create an array of pointers to Foo" then you need to set each pointer to point somewhere. Currently you copy and dereference uninitialized pointers. Commented Nov 23, 2020 at 4:29
  • yeah i think i got a bit confused between arrays of pointers vs arrays of structs Commented Nov 23, 2020 at 4:29
  • Making your array of pointers (the bar elements) point to a local variable in the function is not going to work well once the function returns. You'd be better off setting those pointers to NULL. Commented Nov 23, 2020 at 4:31
  • is there no way to give them values within the method body? @JonathanLeffler Commented Nov 23, 2020 at 4:32

1 Answer 1

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

typedef struct foo
{
    int *bar;
} Foo;

Foo **fooPointers()
{
    Foo **test = malloc(sizeof(Foo*) * 3);  // should be `sizeof(Foo*)`
    
    static int k[] = {0,1,2};  // new array

    for(int j=0;j<3;j++)
    {
        test[j] = malloc(3*sizeof(Foo));  // No need to cast output of malloc
    }

    for (int i = 0; i < 3; i++)
    {
        Foo *curr = *(test + i);
        //int *num = &i;
        curr->bar = &k[i]; // storing different addresses.
    }
    return test;
}

int main()
{
    Foo **kk;

    kk = fooPointers();

    for(int i=0;i<3;i++)
    {
        printf("%d\n", *(kk[i]->bar));  //printng the values.
    }
    return 0;
}

The output is :

0
1
2
Sign up to request clarification or add additional context in comments.

11 Comments

is sizeof(Foo*) equivalent to 1?
also, i recall someone commented and said im just copying and dereferencing pointers, so does your code modify the pointer array being returned?
sizeof(Foo*) is 8, since it is a pointer (Mine 64 bit system). Note : structure contains only one integer pointer.
i just ran the code @ repl.it/languages/c, isn't it supposed to print 0, 1, 2?
No. We are assigning the address of i in the fooPointers . By the time the function returns, the value at the address of i is 3
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.