Thread: Writing to multi-d array.

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    38

    Writing to multi-d array.

    I'm new to multidimensional arrays and I just don't understand how to write into them.. help?



    Code:
    int main(void)
    {
    	int nums[3][5];
    	int i,j;
    
    	printf("Enter 3 sets of 5 double numbers: ");
    	for(i=0; i < 3; i++)
    	{
    		for(j=0; j<5; j++)
    			scanf("%d", nums[i][j]);		
    		
    	}
    
    	for(i=0, j; i<3,j<5;i++,j++)
    	printf("%ld",nums[i][j]);
    					
    	
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > scanf("&#37;d", nums[i][j]);
    If this doesn't complain, then bump up the compiler warning level - you forgot the &

    Eg.
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 bar.c
    bar.c: In function `main':
    bar.c:11: warning: format argument is not a pointer (arg 2)
    bar.c:15: warning: left-hand operand of comma expression has no effect
    bar.c:16: warning: long int format, int arg (arg 2)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    38
    oh wow such a small mistake to me forever .. thanks a lot now works

    Code:
    int main(void)
    {
    	int nums[3][5];
    	int i,j;
    
    	printf("Enter 3 sets of 5 double numbers: ");
    	for(i=0; i < 3; i++)
    	{
    		for(j=0; j<5; j++)
    			scanf("%d", &nums[i][j]);		
    		
    	}
    
    	for(i=0; i < 3; i++)
    	{
    		for(j=0; j < 5; j++)
    			printf("%2d\n",nums[i][j]);
    	}
    					
    	
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a C program for class....
    By Bizmark in forum C Programming
    Replies: 10
    Last Post: 11-13-2007, 10:31 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  5. ? passing multi dimensional array to function
    By rc7j in forum C++ Programming
    Replies: 1
    Last Post: 02-10-2002, 02:19 AM