problem in reading value of 2 D array

This is a discussion on problem in reading value of 2 D array within the C Programming forums, part of the General Programming Boards category; In the program the 2 D array whose rows and columns are to be given at run time has to ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    3

    problem in reading value of 2 D array

    In the program the 2 D array whose rows and columns are to be given at run time has to be displayed as a spiral i.e. from outer rows and columns to inner ones in clockwise direction .
    The code is not working for rows > 4 and columns > 4 .

    The scanf function is not reading correct value in the pointer.

    Logic of program ( for clarity of source ):
    1) store value in 2D array using pointer
    2) display top row
    3) storing its remaining values in another 2D array ( strored such that it spins as well )
    4) displaying top row of this array
    5) continuing till columns are more than 1.


    Code:
    #include<stdio.h>
    #include<conio.h>
    
    void main()
    {
    	int **ptr,**ptr1,i,j,row1,row,col;
    	ptr1=NULL;
    
    	clrscr();
    
    	printf("\nenter no of rows ");
    	scanf("&#37;d",&row);
    
    	printf("\nenter no of columns ");
    	scanf("%d",&col);
    
    	printf("\nenter matrix\n");
    
    	for(i=0;i<row;i++)
    	{
    		for(j=0;j<col;j++)
    		{
    			scanf("%d", &ptr[i][j] );  /* not reading correct value of input */
    		}
    	}
    
    	while(row>0 && col>0)
    	{
    		for(j=0;j<col;j++)
    			printf("%d   ",ptr[0][j]);
    
    		if(row>1)
    		{
    			for(i=0;i<col;i++)
    			{
    				for(j=0;j<row-1;j++)
    				{
    					ptr1[i][j]=ptr[j+1][(col-1)-i];
    				}
    			}
    		}
    		row1=row;
    		row=col;
    		col=row1-1;
    		ptr=ptr1;
    	}
    	getch();
    }
    Last edited by Salem; 09-24-2008 at 11:34 AM. Reason: remove redundant php tags

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And where do you set aside memory for ptr to point to?

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    3
    I did try to set aside memory ( plz. let me know if there's some error )

    int **ptr,**ptr1,i,j,row1,row,col;
    ptr1=NULL;
    ptr1=(int **) malloc(sizeof(int));
    Code is still not working .

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You have set aside enough memory for ... one int. Unless your 2D array is very very very very small, it will not fit inside one integer.

    You should search for dynamic allocation of 2D arrays.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    3
    I did try to allocate larger memory but even that did not worked. There is some problem in assigning the value of ptr1 to ptr I think .

    ptr=(int **) malloc(row*col*sizeof(int));

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, that allocates enough memory. But the way a pointer to pointer works, if you do ptr[i][j] or some such, it will go look for a pointer at location ptr+i, and that's most likely not a valid pointer to the location you want.

    As suggested, go look for an example of 2D array allocation.

    Also, in C, you should NOT cast the result of malloc(), as it can hide hard to find bugs.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    A bit off topic but casting of the return value of malloc() is quite normal. It prevents compiler warnings about mismatched data types especially at level-4 (highest persnickety setting).

    As long as the appropriate headers are included (function prototypes) such that physically the proper sized value is returned.

    You can not expect malloc()'s result to match the data type for your needs. It's not clairvoyant.
    Last edited by nonoob; 09-26-2008 at 08:20 AM.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You tell malloc how many bytes you'd like, what you do with the memory is upto you. implicit void promotion is legal, hell that's one reason why void exists.

    > mismatched data types especially at level-4 (highest persnickety setting).
    Between void and x...?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Quote Originally Posted by nonoob View Post
    A bit off topic but casting of the return value of malloc() is quite normal. It prevents compiler warnings about mismatched data types especially at level-4 (highest persnickety setting).
    Not in C. If it does, then it's a broken compiler/warning.
    Implicit conversion from void* to T* is perfectly valid in C (though not C++, where a cast is required).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    37
    use this,

    Code:
      ptr = (int**)malloc(row*sizeof(int*));// allocates memory for rows
    
      for(count=0;count<row;count++)
          ptr[count]= (int*)malloc(column*sizeof(int)); //allocates memory for columns at each row
    now you can use ptr[][] just as a 2D array.
    Last edited by san_crazy; 09-26-2008 at 01:05 PM.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I find that I ask this question far too often.....

    Code:
    {[0,1,2,3,4,5],
     [6,7,8,9,A,B]}
    
        ^ That is my 2d array.
    
     [0,1,2,3,4,5,
      6,7,8,9,A,B]
    
        ^ That is my 1d array.
    I notice that given my visual, that if I align my array up in rows like that that they look exactly the same. So if the row is a constant size.

    Example:
    Code:
    #define ROWS 30
    
    // later in your code.
    
    int x = 0, y = 1;
    int *ptr = malloc(ROWS*2); // 2d array, right
    
    
    ptr[y*ROWS + x] = 0; // This would be the same as ptr[1][0] = 0

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Quote Originally Posted by san_crazy View Post
    use this,

    Code:
      ptr = (int**)malloc(row*sizeof(int*));// allocates memory for rows
    
      for(count=0;count<row;count++)
          ptr[count]= (int*)malloc(column*sizeof(int)); //allocates memory for columns at each row
    now you can use ptr[][] just as a 2D array.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 06:07 AM
  2. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 05:19 AM
  3. problem copy from array to another
    By s-men in forum C Programming
    Replies: 3
    Last Post: 09-07-2007, 02:51 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21