Hi,

I am starting to learn C programming. I recently stumbled across a structure below
#include <stdio.h>
#include <stdlib.h>

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



typedef struct
{
  int value;
} fteid_t;


typedef struct {
  fteid_t                       *fteid; // Optional Ins 0
} context_t;


typedef struct {
  context_t     default_bearer; 
} send_request;

So I tried to create a function that can basically assign the value of fteid.value. I believe this is not as simple as fteid.value, because the fteid_t is located inside context_t, and context_t is inside send_request (some sort of list, but no next/prev pointer?)
How can I fix the method below? (I believe the problem is on line 8)

Thank you

void send_Message(send_request *msg)
{       
        printf("IN THIS BLOCK\n\n");
        
        context_t *fteid = 0;
        fteid = malloc(sizeof(context_t));

        msg->default_bearer.fteid->value = 1;
        free(fteid);
}

int main()
{       
        
        send_request *msg = 0;
        msg = (send_request *) malloc(sizeof(send_request));

        send_Message(msg);
        free(msg);
        return 0;
}

You don't need lines 5, 6 and 9 because fteid is never used for anything. That is not the same as the fteid on line 8.

Edited 5 Years Ago by Ancient Dragon: n/a

You don't need lines 5, 6 and 9 because fteid is never used for anything. That is not the same as the fteid on line 8.

yes, I tried to delete those line, still segfault.. any suggestion?

this fixes it

void send_Message(send_request *msg)
{       
        printf("IN THIS BLOCK\n\n"); 
        msg->default_bearer.fteid = malloc(sizeof(fteid_t));
        msg->default_bearer.fteid->value = 1;
}

Edited 5 Years Ago by Ancient Dragon: n/a

this fixes it

void send_Message(send_request *msg)
{       
        printf("IN THIS BLOCK\n\n"); 
        msg->default_bearer.fteid = malloc(sizeof(fteid_t));
        msg->default_bearer.fteid->value = 1;
}

Thank you , it works.

This question has already been answered. Start a new discussion instead.