Hi all,

I have a simple code below

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


typedef struct {
        int apn_in_use;
} bts_pdn_con_t;

typedef struct _bts_pdn_con_list_t_slot {
        bts_pdn_con_t conn;
        struct _bts_pdn_con_list_t_slot *next, *prev;
} bts_pdn_con_list_t_slot;

typedef struct {
        bts_pdn_con_list_t_slot *head, *tail;
} bts_pdn_con_list_t;

typedef struct {
        int umsi;
        bts_pdn_con_list_t connections;
} bts_ue_t;




int funct(bts_ue_t *ue)
{

	bts_pdn_con_t *conn = 0;
	conn = malloc(sizeof(bts_pdn_con_t));

	printf("value of umsi is %d\n", ue->umsi);
	printf("value of apn is %d\n", conn->apn_in_use);

return 1;
}




int main()
{
	bts_ue_t ue;
	memset(&ue, 0, sizeof(ue));

	bts_pdn_con_list_t_slot bts_slot;
	memset(&bts_slot, 0, sizeof(bts_slot));
	bts_pdn_con_t *conn = &bts_slot.conn;


	ue.umsi = 1;
	conn->apn_in_use = 2;

	printf("value of apn is %d\n", conn->apn_in_use);

	funct(&ue);

return 1;
}

In that function, I assign umsi with 1, and apn_in_use with 2. When I pass it to funct(&ue), I got the value of umsi correctly, but for the apn, I got the wrong value (seems like garbage). How am I supposed to get the value of conn->apn_in_use in the function int funct(bts_ue_t *ue). I can only pass ue, but I need the conn->apn_in_use correctly..

thank you for the explanation

When I pass it to funct(&ue), I got the value of umsi correctly, but for the apn, I got the wrong value (seems like garbage).

It is garbage. In funct(), you're creating a new object of bts_pdn_con_t and leaving it uninitialized. What you want to do, I suspect, is print the value of ue->conn rather than create a new conn:

int funct(bts_ue_t *ue)
{
    bts_pdn_con_list_t_slot *it = ue->connections.head;

    printf("value of umsi is %d\n", ue->umsi);

    while (it != NULL)
        printf("value of apn is %d\n", it->conn.apn_in_use);

    return 1;
}

But that won't immediately fix your problems as the setting up of ue in main is totally jacked up.

interesting code that I see. As I understand, that is supposed to be doubly linked list, and usually people use pointer to struct object ( I mean, in this case :

bts_ue_t ue;
	memset(&ue, 0, sizeof(ue));

	bts_pdn_con_list_t_slot bts_slot;
	memset(&bts_slot, 0, sizeof(bts_slot));
	bts_pdn_con_t *conn = &bts_slot.conn;

would you mind telling what is the idea for these struct? why do you define for instance bts_slot (and not as a pointer var *bts_slot) :)

thanks.. it works. but just wondering, what is the idea behind this

while (it != NULL)
        printf("value of apn is %d\n", it->conn.apn_in_use);

. I tried that it will give segmentation fault if I do not put while. but on the other way, the it will not be zero isn't it? (as it point to connections.head) ? Or?

Oops, I forgot to increment it . That loop is for traversing the list:

while (it != NULL) {
    printf("value of apn is %d\n", it->conn.apn_in_use);
    it = it->next;
}

Of course, that's assuming you're terminating your linked list with NULL, and there's no dummy head.

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.