Thread: 2D array pointers (almost got it)

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    62

    2D array pointers (almost got it)

    I am trying to get this to work. I think I am close.

    I am asking users to input for a matrix.

    num rows and num columns;

    I then want them to input each value that they want in each row.
    It complies, and I am pretty sure the pointers are allocated correctly.
    I think the loop is not inputing each user input for the values.

    Code:
    #include "stdafx.h"
    #include "math.h"
    
    int main(int argc, char* argv)
    {
    	int i,j;
    	int **matrix = NULL;
    	int numRows;
    	int numCol;	
    	
    	printf( "Enter the number of    rows:  ");
    	scanf( " %d", &numRows);
    	printf( "Enter the number of columns:  ");
    	scanf( " %d", &numCol);	
    	
    	matrix = malloc(numRows * sizeof(*matrix));
    	
    	for (i = 0; i < numRows; i++)
    	{
    		matrix[i] = malloc(numCol * sizeof(*matrix[i]));
    		printf( "Enter the values in row %d: ", i + 1);
    		for (j = 0; j < numCol; j++)
    		{
    			matrix[i][j] = getchar();		
    		}
    	}
    
    	for (i = 0; i < numRows; i++)
    	{
    		for (j = 0; j < numCol; j++)
    		{
    			printf("%d",matrix[i][j]);
    		}
    		printf("\n");
    	}
    	
    	for (i = 0; i < numRows; i++)
    	{
    		free(matrix[i]);
    	}
    	
    	free(matrix);	
    	return 0;
    }
    I think this is not efficient
    Code:
    matrix[i][j] = getchar();
    Am I blind??

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    well I got it to work I changed:

    Code:
    matrix[i][j] = getchar()
    to

    Code:
    scanf(" %d", &userNum);
    matrix[i][j] = userNum;
    and I think it is efficient from a memory perspective.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    First, argv should be a char**, not a char*. Typo, I imagine, and one that probably has no effect on your program.

    You've run into a very common problem when doing user input in C. If you do
    Code:
    scanf("%d", &x);
    Generally the user will type a number and hit enter. %d will read the number, but it will not read the enter (which comes through as a '\n', or newline character). That newline is still hanging around waiting to be read when you call getchar(). getchar() doesn't discriminate, so it reads the newline and returns it. But guess what the user did when you asked for a character? He gave the character and hit enter. The character will be read by the next getchar(), but the newline will be read by the getchar() after that. And so on.

    You could use scanf() with %d to read in values for your matrix. %d will eat up any initial whitespace it sees, so all those enters will disappear. This is sort of a band-aid, though, because if the user types in something alphabetic, scanf() will panic and not read anything. This is why it's always smart to check the return value of scanf(), and why it's usually smarter to abandon scanf() completely.

    The generally suggested method to read input is to read a line and parse it. The user can type whatever he pleases. Instead of hoping that what he typed is valid, you just read a bunch of it and check. Use fgets() to read a line; it's not perfect but it goes a long way to making your code more robust.

  4. #4
    Devil™
    Join Date
    Oct 2007
    Location
    IIT-Kharagpur, India
    Posts
    104
    Quote Originally Posted by tikelele View Post
    well I got it to work I changed:

    Code:
    matrix[i][j] = getchar()
    to

    Code:
    scanf(" &#37;d", &userNum);
    matrix[i][j] = userNum;
    and I think it is efficient from a memory perspective.
    I find faq to be useful in many cases before posting
    well in your case these links will help youI wish you go through those. the faq given there are real FAQs
    If not now, they might be useful someother day
    Last edited by gibsosmat; 11-07-2007 at 11:36 PM. Reason: typo
    C's Motto: who cares what it means? I just compile it!!

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    Quote Originally Posted by gibsosmat View Post
    I find faq to be useful in many cases before posting
    well in your case these links will help youI wish you go through those. the faq given there are real FAQs
    If not now, they might be useful someother day
    Once again...I didn't see the forest for the trees!!!

    Thanks gibsosmat, the help and the FAQ's here are excellent!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM