# Thread: how long this "story": "there is another integer and a pointer" continues...?

1. ## how long this "story": "there is another integer and a pointer" continues...?

there is one thing i want to know about all this stuff! Let' s say we are in A [1] [3] cell. There is probably an integer named 'x' and a pointer named next which shows in another struct Data, i think. lets name this struct data 2. ok so far... in struct data 2 there is another integer and a pointer? how long this "story": "there is another integer and a pointer" continues...? ? ?
it is a bit confusing...
Code:
```#include <stdio.h>
#include <stdlib.h>

typedef struct Data{
int x;
struct Data *next;
}data;

int main(){
int size, i, j;
data **A;
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");

return 0;
}

/*
Output:

Type the size of the array:5

A[0][0]: 0	A[1][0]: 0	A[2][0]: 0	A[3][0]: 0	A[4][0]: 0
A[0][1]: 0	A[1][1]: 1	A[2][1]: 0	A[3][1]: 0	A[4][1]: 0
A[0][2]: 0	A[1][2]: 2	A[2][2]: 0	A[3][2]: 0	A[4][2]: 0
A[0][3]: 0	A[1][3]: 3	A[2][3]: 0	A[3][3]: 0	A[4][3]: 0
A[0][4]: 0	A[1][4]: 4	A[2][4]: 0	A[3][4]: 0	A[4][4]: 0

A[0][0]: (nil)	A[1][0]: (nil)	A[2][0]: (nil)	A[3][0]: (nil)	A[4][0]: (nil)
A[0][1]: (nil)	A[1][1]: (nil)	A[2][1]: (nil)	A[3][1]: (nil)	A[4][1]: (nil)
A[0][2]: (nil)	A[1][2]: (nil)	A[2][2]: (nil)	A[3][2]: (nil)	A[4][2]: (nil)
A[0][3]: (nil)	A[1][3]: (nil)	A[2][3]: (nil)	A[3][3]: (nil)	A[4][3]: (nil)
A[0][4]: (nil)	A[1][4]: (nil)	A[2][4]: (nil)	A[3][4]: (nil)	A[4][4]: (nil)

*/```

2. What you have there is an array of linked lists, where each list is the exact same size as the array of lists. I suggest you look up linked lists or look at this post where I explained to someone else the same concept:

Pointer to struct...

3. can this question be answered simply...? i mean how "deep" in the list can we go on the above code....? how many pointers to pointers to pointers to pointers etc do we have?

4. Oh hold on. I just realized you are the same person LOL

So you have size pointers to the struct type. And each of these is the head of a list with size nodes.
So, vertically you have size pointers. And horizontally you have another size pointers.

ptr1 -> .... -> .... -> ..... -> (size nodes)
ptr2 -> .... -> .... -> ..... -> (size nodes)
ptr3 -> .... -> ..... -> .....-> (size nodes)
....
ptr#size ->....->....->.....->(size nodes)

5. nevermind... i just thought to change a bit my code as an example
:/ i hope i got it...
So, all is about "size" ? i mean the number of pointers vertically i have is equal to the size..right?

6. 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;
}```

7. It looks like I was mistaken. You don't actually have a list horizontally because you never "link" the elements using the next pointer. You get a segfault because A[0][0]->next points to some rubbish NOT to A[0][1].

8. So, if i wanted to connect this also horizontally what would i do?

9. You would have to loop through each element of the horizontal arrays and set the next pointer to set to the next element.

A[0][0] ->next = A[0][1]; A[0][1]->next= A[0][2]......
A[1][0]->next = A[1][1]; A[1][1]->next = A[1][2]....
......................

10. I still don't get why you are using this list-array hybrid structure.

11. Originally Posted by claudiu
I still don't get why you are using this list-array hybrid structure.
there may be no point...lol look, i am trying to learn how this thing is connected so i started from an example of my mind and i still try to complete...nevermind....

why are you using -> and not .(dot)
when we are accessing something from a struct we are using . right?

12. Yes it should be dot. I got confused.

13. So, with this loop it is ok you think?

Code:
```		for(i=0; i<size; i++){
for(j=0; j<size; j++){
A[i][j].next = &A[i][j+1];
}
}```

14. j < size -1 in the second for.

As it, is when j is on the last position(size-1) you are accessing A[i][j+1] which is A[i][size] which doesnt exist.

15. Originally Posted by claudiu
j < size -1 in the second for.

As it, is when j is on the last position(size-1) you are accessing A[i][j+1] which is A[i][size] which doesnt exist.
correct ! oh...it is 9 o clock in the morning in greece and i havent sleep at all at the night...so if i continiue i' ll do terible mistakes...
hey, thanks a lot for your help
cya!