Thread: array declaration

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    array declaration

    i defined the array inside an if function. please guide me as to how am i wrong. THANK U.
    Code:
    	else if (op[6] == 'q')
    		{
    			printf("you have selected Matrix function \n");
    		    printf("please key in the dimension of the matrix");
    			scanf("%i %i",&b,&t);
    	        
    			int m[b][t];
    			
    			for(x=0;x<b;x++)
    			{
    			for(y=0;y<t;y++)
    			{
    			printf("please enter the values of matrix");
    			scanf("%i",&c);
    			fflush(stdin);
    			m[b][t]=c;
    			printf("The matrix entered",m);
    			}
    			}
    		}
    Last edited by Salem; 12-18-2010 at 01:44 AM. Reason: Added [code][/code] tags - learn to use them yourself

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    m[b][t] is not a valid place in your array, as you have defined it. The first index needs to be some number less than b (b is too big), and the second element needs to be some number less than t (t is too big).

  3. #3
    Registered User spygg's Avatar
    Join Date
    Dec 2010
    Location
    Chengdu China
    Posts
    2
    You can't use variable as the array index

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by spygg View Post
    You can't use variable as the array index
    If you mean for a size declaration, you can in C99.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    A few more things:
    1. Don't fflush stdin: Cprogramming.com FAQ > Why fflush(stdin) is wrong
    2. Read the printf documentation, the following is invalid:
      Code:
      printf("The matrix entered",m);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. not constant satic array declaration
    By fcommisso in forum C Programming
    Replies: 6
    Last Post: 12-14-2009, 03:01 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM

Tags for this Thread