Problem with variable length array

This is a discussion on Problem with variable length array within the C Programming forums, part of the General Programming Boards category; Well here's the code : Code: #include <stdio.h> #include <stdbool.h> int main (int argc, const char * argv[]) { // ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    37

    Problem with variable length array

    Well here's the code :

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    int main (int argc, const char * argv[]) {
        // insert code here...
    	int size;
    	int row = 0;
    	int column = 2;
        
    	//get user input
    	printf("This program creates a magic square of a specific size.\n");
    	printf("The size must be an odd number between 1 and 99.\n");
    	printf("Enter size of magic square: ");
    	scanf("%d", &size);
    	
    	int myArray[size][size];
    	
    	for (int rows = 0; rows < size; rows++) {
    		for (int columns = 0; columns < size; columns++) {
    			myArray[rows][columns] = 0;
    		}
    	}
    	
    	
    	myArray[0][2] = 1;
    }
    The problem is that when I debug the program (using xcode) I can't see the values of myArray (I get -1 instead).
    Am I doing something wrong?
    Thank you.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What compiler are you using?

    I'm not aware of any C compiler that allows the user to input sizes for an array, without using malloc() or calloc(). (which means also including stdlib.h).

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    37
    I am using gnu99 compiler.I am reading a book and it says that in c99 it is ok to use variable length arrays.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well, now I am jealous!

    Do you need to include another header file to use the variable array size, feature? Maybe try including stdlib.h?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    PellesC does this too... I believe it's a standard part of c-99.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    VLA is C99 feature.
    Ancient compiler like turbo C doesn't support C99. FYI.
    The problem is that when I debug the program (using xcode) I can't see the values of myArray (I get -1 instead).
    Am I doing something wrong?
    What do you mean by 'I can't see the values of myArray'??
    Last edited by Bayint Naung; 11-06-2010 at 07:01 AM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by CommonTater View Post
    PellesC does this too... I believe it's a standard part of c-99.
    I'm going to be investigating Pelles C in the near future, primarily for 64 bit and VLA features.

    Ancient compiler like turbo C doesn't support C99. FYI.
    After a few decades with Turbo C, I've got it figured out that it may *never* be able to handle, either one of these features! <lol>

    What do you mean by 'I can't see the values of myArray'??
    The myArray on his system, seems to be lacking reflected light in sufficient luminosity.
    Last edited by Adak; 11-06-2010 at 07:29 AM.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    I'm going to be investigating Pelles C in the near future, primarily for 64 bit and VLA features.
    LOL... well, my friend, there is a reason I keep recommending it....
    I can't wait for your reaction when you see the libraries it comes with.


    After a few decades with Turbo C, I've got it figured out that it may *never* be able to handle, either one of these features! <lol>
    No worries... I like dinosaurs too...

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    37
    Quote Originally Posted by Bayint Naung View Post
    VLA is C99 feature.
    Ancient compiler like turbo C doesn't support C99. FYI.

    What do you mean by 'I can't see the values of myArray'??
    http://i51.tinypic.com/2ekko5g.png

  10. #10
    Registered User
    Join Date
    Jul 2010
    Posts
    37
    Ok I solved the problem using this code : (I can't see the array elements in debugging mode but it works)

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    int main (int argc, const char * argv[]) {
        // insert code here...
    	int size;
    	int row = 0;
    	int column = 2;
    	int rows;
    	int columns;
        
    	//get user input
    	printf("This program creates a magic square of a specific size.\n");
    	printf("The size must be an odd number between 1 and 99.\n");
    	printf("Enter size of magic square: ");
    	scanf("%d", &size);
    	
    	int myArray[size][size];
    	
    	for (rows = 0; rows < size; rows++) {
    		for (columns = 0; columns < size; columns++) {
    			myArray[rows][columns] = 0;
    		}
    	}
    	
    	
    	myArray[row][column] = 1;
    	
    	for (int counter = 2; counter <= size * size; counter++) {
    		if (row == 0)
    			row	= size -1;
    		else
    			row = row - 1;
    
    		if (column == size - 1)
    			column = 0;
    		else
    			column = column + 1;
    		
    		if (!myArray[row][column] == 0)
    		{
    			row = (row + 2) % size;
    			if (column == 0)
    				column = size - 1;
    			else
    				column = column - 1;
    		}
    		
    		myArray[row][column] = counter;
    	}
    	
    	for (rows = 0; rows < size; rows++) {
    		for (columns = 0; columns < size; columns++) {
    			printf("%2d ", myArray[rows][columns]);
    		}
    		
    		printf("\n");
    	}
    }

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well done, Skiabox!

    I'd think about replacing that compiler, though. Not being able to see the values of an array of any type, in your debugger, is unacceptable.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    What an awful hack-job.
    That's not an answer, it's a mess.

    Does this work for you?
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    int main (int argc, const char * argv[]) {
        // insert code here...
        int size;
        int row = 0;
        int column = 2;
        int rows;
        int columns;
    
        //get user input
        printf("This program creates a magic square of a specific size.\n");
        printf("The size must be an odd number between 1 and 99.\n");
        printf("Enter size of magic square: ");
        scanf("%d", &size);
    
        int myArray[size][size];
    
        for (rows = 0; rows < size; rows++) {
            for (columns = 0; columns < size; columns++) {
                myArray[rows][columns] = 0;
            }
        }
    
    
        myArray[row][column] = 1;
    
        for (rows = 0; rows < size; rows++) {
            for (columns = 0; columns < size; columns++) {
                printf("%d ", myArray[rows][columns] );
            }
            printf("\n");
        }
      return 0;
    }
    
    $ gcc foo.c
    $ ./a.out 
    This program creates a magic square of a specific size.
    The size must be an odd number between 1 and 99.
    Enter size of magic square: 5
    0 0 1 0 0 
    0 0 0 0 0 
    0 0 0 0 0 
    0 0 0 0 0 
    0 0 0 0 0
    Nevermind what the debugger is saying, do you get the right output?

    FWIW, I think it's a GDB bug, not a compiler bug.
    C99 variable length arrays

    I just tried it with my gdb, and it too has trouble working out what the effective type of a VLA is.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with getting correct string length
    By yukimurasanada in forum C Programming
    Replies: 1
    Last Post: 11-15-2009, 08:21 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 05:07 AM
  4. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 04:19 AM
  5. Determine length of an array
    By ulillillia in forum C Programming
    Replies: 7
    Last Post: 04-21-2007, 08:32 PM

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