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

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    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. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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...
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    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. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Oh hold on. I just realized you are the same person LOL

    Sorry about that.

    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)
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    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. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    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. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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].
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    So, if i wanted to connect this also horizontally what would i do?

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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]....
    ......................
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I still don't get why you are using this list-array hybrid structure.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  11. #11
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Quote Originally Posted by claudiu View Post
    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. #12
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Yes it should be dot. I got confused.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  13. #13
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    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. #14
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  15. #15
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Quote Originally Posted by claudiu View Post
    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!

Popular pages Recent additions subscribe to a feed