0

I am passing a structure to a function. This structure contains a pointer variable. The structure is itself passed to the function as a pointer variable. I am unable to access the structure's pointer variable member (while inside the function) using subscript style of accessing an array (var[0]). The code compiles but I get a segmentation fault upon executing the .o file. The error lies in the first line of the function code. I am not able to access the pointer variable inside the structure.

I can easily access this pointer variable using the dot operator when functions are not involved. For example, see the working code in the end.

Problematic code:

#include<stdio.h>

struct prog
    {
        double* var;
        char* name;
        int arr[]; 
    };

typedef struct prog p;

void fun(p *inp);

int main()
{
    p in;

    p *inptr = &in;

    in.arr[1] = 32;

    fun(inptr);


    printf("Value var is: %f\n", in.var[0]);
    printf("Value arr[0] is: %d\n", in.arr[0]);
    printf("Value arr[1] is: %d\n", in.arr[1]);
    printf("Name of prog is: %s\n", in.name);
    return 0;
}

//Function
void fun(p *inp)
{
    (*inp).var[0] = 4.5;
    //inp->var[0] = 4.5;
    inp->arr[0] = 11;
    inp->arr[1] = 55;
    inp->name = "User";
}

Working code:

#include<stdio.h>

struct prog
    {
        double* var;
        char* name;
        int arr[]; 
    };

void main()
{
    struct prog p;
    p.var[0] = 3.7;
    p.name = "User";
    p.arr[0] = 100;
    p.arr[1] = 30;  

    printf("Value var is: %f\n", p.var[0]);
    printf("size of prog is: %d bytes \n", sizeof(p));  
    printf("Value at arr[0] is: %d\n", p.arr[0]);
    printf("Value at arr[1] is: %d\n", p.arr[1]);
    printf("Name of prog is: %s\n", p.name);
}

Running 'gdb' says "Program received signal SIGSEGV, Segmentation fault. 0x00005555555551d3 in fun (in=0x7fffffffe200) at strct-fun.c:35 35 (*inp).var[0] = 4.5;"

  • You haven't allocated memory for any of your pointers or array. – haccks Sep 09 '19 at 08:53
  • @haccks : If that is the case then why should the tagged 'Working code" work ? I am trying to extend it to functions. Could you run it as a C code? Thanks for replying by the way. – rupak rokade Sep 09 '19 at 10:07

1 Answers1

0
in.arr[1] = 32;

You can not do that using static memory, if you want a flexible array member you need to malloc:

p *in = malloc(sizeof *in + sizeof(int) * number_of_arr_elements);

Notice that flexible arrays only works for the last member.

You need space for the other members: var and name too:

p->var = malloc(sizeof(double) * number_of_var_elements);
p->name = malloc(max_length_of_name + 1); /* +1 for the trailing \0 */
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • Thanks for replying. I tried however commenting out the problematic lines (first two lines of fun() and first print statement in main). Turns out the code does work fine i.e. no problem doing in.arr[1] = 32; Can you please try running it as a C code? – rupak rokade Sep 09 '19 at 09:56
  • Have you tried my approach? It should work – David Ranieri Sep 09 '19 at 10:26
  • Thank You. Your solution of using malloc for "var" and "name" worked. I couldnt get the malloc for *in working though. I have a followup question that requires me to show a code but I am unable to paste it here. I shall ask a new question. Thank You. – rupak rokade Sep 09 '19 at 12:12