2D arrays in pointer fashion issues

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • momotaro
    Contributor
    • Sep 2006
    • 357

    2D arrays in pointer fashion issues

    I have a piece of code that is supposed to fill a 2D array and print it out using pointers, the problem in stead I get only a part of it correct!
    here is the code:
    Code:
    #include <stdio.h>
    
    #define MAXROW 2
    #define MAXCOL 2
    
    main()
    {
    	int i, j, T[MAXROW][MAXCOL];
    
    //filling
    	for(i = 0; i < MAXROW; i++)
    	{
    		for(j = 0; j < MAXCOL; j++)
    		{
    			printf("Please enter your value (%d,%d): \n", i, j);
    			scanf("%d", T+i+(j*MAXROW));
    		}
    	}
    
    //prompting
    	for(i = 0; i < MAXROW; i++)
    	{
    		for(j = 0; j < MAXCOL; j++)
    			printf("%d\t", T[i][j]);
    		printf("\n");
    	}
    	getch();
    }
    example:

    I fill it with 1 2 3 4 I get: 1 -xxxxxx 3 -xxxxxx

    Please help
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Refer to Arrays Revealed for general information about 2D arrays.

    Comment

    • JonathanS
      New Member
      • Jan 2008
      • 37

      #3
      in your scanf, your T should be T[0] (so that your pointer is correct). To correct your other mistake, draw your array out on a piece of paper from the point of view of how it is stored in memory.

      Comment

      • momotaro
        Contributor
        • Sep 2006
        • 357

        #4
        Jonathans thx for the help but you didn't get my pointer :p I mean my point :d
        am using JUST pointers

        thx anyway

        Comment

        • JonathanS
          New Member
          • Jan 2008
          • 37

          #5
          T[0] is a pointer since this is a 2-D array . If you want to you could substitute &T[0][0] (see the Arrays Revealed that Don had recommended).

          I was commenting before that you have the calculation i+j*MAXROW, but that's not how arrays are laid out in memory, where the column is offset into it's particular row.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Do you mean that you don't want to do this?

            scanf("%d", &T[i][j]);

            Which is the obvious solution.

            Your problem with

            T+i+(j*MAXROW)

            is that the arithmetic is wrong.

            T[MAXROW][MAXCOL]

            is not MAXCOL array of MAXROW arrays but the other way around MAXROW array of MAXCOL arrays.

            Put another way if you have an array

            char T[2][3];

            the memory map of elements in the array looks something like

            Code:
            -------------------------
            |T[0][0]|T[0][1]|T[0][2]|
            -------------------------
            |T[1][0]|T[1][1]|T[1][2]|
            -------------------------
            but the way you have coded it it would have to be like this

            Code:
            -----------------
            |T[0][0]|T[1][0]|
            -----------------
            |T[0][1]|T[1][1]|
            -----------------
            |T[0][2]|T[1][2]|
            -----------------
            to work

            Comment

            • vipin sharma
              New Member
              • Jul 2009
              • 16

              #7
              hello,

              I have slightly modified given code. When I am using T[i] +j to display address, i am getting correct addresses of array T in output.

              But when I am using T + i*MAXCOL +j (mathematically this is correct), I am getting incorrect answer. In the output instead of getting 4 bytes increment one by one to base address I am seeing 8 bytes increment.

              Can anyone explain why it is acting differently?

              Code:
              #include <stdio.h>
               
              #define MAXROW 2
              #define MAXCOL 2
               
              int main()
              {
                  int i, j, T[MAXROW][MAXCOL];
               
              	
              
              	for(i = 0; i < MAXROW; i++)
                  	{
                      	for(j = 0; j < MAXCOL; j++)
                      	    printf("%x\t", &T[i][j]);
                      	printf("\n");
                  	}
              
              //filling
              
              	printf("\nUSING T[i]+j\n");
                  for(i = 0; i < MAXROW; i++)
                  {
                      for(j = 0; j < MAXCOL; j++)
                      {
                          printf("Address of  (%d,%d): ", i, j);
                          printf("%x\t", T[i]+j);
                      }
              	printf("\n");
                  }
               
              	printf("\nUSING T+i*MAXCOL +j\n");
                  for(i = 0; i < MAXROW; i++)
                  {
                      for(j = 0; j < MAXCOL; j++)
                      {
                          printf("Address of  (%d,%d): ", i, j);
                          printf("%x\t", T+ i*MAXCOL +j);
                      }
              	printf("\n");
                  }
              /*
              //prompting
                  for(i = 0; i < MAXROW; i++)
                  {
                      for(j = 0; j < MAXCOL; j++)
                          printf("%d\t", T[i][j]);
                      printf("\n");
                  }
              //    getch();
              */
              	return 0;
              }

              OUTPUT

              Code:
              bfbeb10c	bfbeb110	
              bfbeb114	bfbeb118	
              
              USING T[i]+j
              Address of  (0,0): bfbeb10c	Address of  (0,1): bfbeb110	
              Address of  (1,0): bfbeb114	Address of  (1,1): bfbeb118	
              
              USING T+i*MAXCOL +j
              Address of  (0,0): bfbeb10c	Address of  (0,1): bfbeb114	
              Address of  (1,0): bfbeb11c	Address of  (1,1): bfbeb124

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                vipin sharma to answer your question you need to ask yourself what type is T interpreted as when used as you have done in pointer arithmetic.

                Here's a clue, it is not int *.

                Comment

                Working...