开发者

passing linkedlist and get value of it

I am very sorry for the confusing code, so I am trying to change the whole question so it is impler :

So basically, I have a predefined struct that I get from a header file, that I put in one main file. I need to create a function that can take the value of value1->identity in my own function.

I am not supposed to pass the value1 as a parameter, as I can access value1 from user struct. but how can i do this in int funct(user *user) so when I print value1->identity in int funct(user *user) it does not return me a garbage value.

Thank you

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

typedef struct {
    int identity;
} value;

typedef struct list1_struct {
    value value1;
    struct list1_struct *next, *prev;
} list_t_slot;

typedef struct {
    list_t_slot *head, *tail;
} list_t;

typedef struct {
    int number;
    list_t value1s;
} value2;

typedef struct list2 {
    value2 conn;
    struct list2 *next, *prev;
} list_t_slot2;

typedef struct {
    list_t_slot2 *head, *tail;
} list_t2;

typedef struct {
    int power;
    list_t2 connections;
} user;

int funct(user *user) {

    return 1;
}

int main() {
    user user;
    memset(&user, 0, sizeof(user));

    list_t_slot2 chron_slot;
    memset(&chron_slo开发者_StackOverflow中文版t, 0, sizeof(chron_slot));
    value2 *conn = &chron_slot.conn;

    list_t_slot value1_slot;
    memset(&value1_slot, 0, sizeof(value1_slot));
    value *value1 = &value1_slot.value1;

    user.power = 1;
    value1->identity = 1;

    printf("value of identity is %d\n", value1->identity);

    funct(&user);

    return 1;
}


Did you notice that you are not using the user variable until the end of funct?

You are working with uninitialized pointers. That's why you are getting different (all equally garbage) results. You have to fix this first.

To begin with:

chron_apn_con_list_t_slot *chron_slot;
chron_apn_con_t *conn = &chron_slot->conn;

This doesn't make any sense. There is no chron_slot object to work with. You either need to allocate one with a malloc or use a stack object by omitting the pointer notation when declaring chron_slot.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜