Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct Data{
int x;
struct Data *next;
}data;
int main(){
int size, i, j, count=0;
data **A;
data *temp;
printf("Type the size of the array:");
scanf("%d", &size);
/* Allocating space for A */
A = malloc(size*sizeof(data*));
for(i=0; i<size; i++){
A[i] = malloc(size*sizeof(data));
}
/* Check if malloc succeded */
if(A == NULL){
printf("allocating space failed.\n");
exit(1);
}
/* Fil the array with zero */
for(i=0; i<size; i++){
for(j=0; j<size; j++){
A[i][j].x = 0;
}
}
/* Fil a column with i */
for(i=0; i<size; i++){
A[1][i].x = i;
}
/* Prints a graph */
for(i=0; i<size; i++){
printf("\n");
for(j=0; j<size; j++){
printf("A[%d][%d]: %d\t", j, i, A[j][i].x);
}
}
printf("\n");
for(i=0; i<size; i++){
printf("\n");
for(j=0; j<size; j++){
if(A[i][j].next == NULL){
printf("A[%d][%d]: %p\t", j, i, A[j][i].next);
}
}
}
printf("\n");
A[0][0].x = 5;
A[0][1].x = 9;
printf("\nA[0][0]: %d\n", A[0][0].x);
printf("A[0][1]: %d\n", A[0][1].x);
temp = A[0][0].next;
printf("o temp deixnei : %p\n", temp);
temp = &A[0][0];
printf("o new temp deixnei : %d\n", (*temp).x);
temp = temp->next;
//temp = temp->next; //why do i have here segmentation fault ?
printf("o new new temp deixnei : %p\n", temp);
return 0;
}