Я пытаюсь получить доступ ко второму двойному указателю, но он сразу обнаруживает ошибки только после доступа к первому. Что происходит?
Кажется, что работает без второго двойного указателя, но я понятия не имею, почему.
#include<stdio.h>
#include<stdlib.h>
struct queue{
int ** x;
int ** y;
};
struct queue funct1(){
struct queue me;
int * x = malloc(sizeof(int));
int * y = malloc(sizeof(int));
*x = 20;
*y = 40;
me.x = &x;
me.y = &y;
return me;
}
int main(void){
struct queue hello;
hello = funct1();
printf("%d\n", *(*(hello.x)));
printf("%d\n", *(*(hello.y)));
}
Ожидаемое: 20 40
Фактическое: 20 Сегментацияошибка: 11
РЕДАКТИРОВАТЬ:
Кажется, все еще не работает. Я добавил этот код в функцию:
int ** xpointer = malloc(sizeof(int*));
int ** ypointer = malloc(sizeof(int*));
*x = 20;
*y = 40;
xpointer = &x;
ypointer = &y;
me.x = xpointer;
me.y = ypointer;
РЕДАКТИРОВАТЬ 2: Это похоже на работу.
struct queue funct1(){
struct queue me;
int * x = malloc(sizeof(int));
int * y = malloc(sizeof(int));
int ** xpointer = malloc(sizeof(int*));
int ** ypointer = malloc(sizeof(int*));
*x = 20;
*y = 40;
*xpointer = x;
*ypointer = y;
me.x = xpointer;
me.y = ypointer;
return me;
}