I am trying to use a vla (variable length array) in my code but I see that the array is not initialized properly when I debug the program.
Here's the 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;
}

and here's what I get when debugging :
[img]http://i51.tinypic.com/2ekko5g.png[/img]

Try setting up your VLA in a function call like so

#include <stdio.h>

void myfunc(int size)
{
	int i = 0;
	int mya[size];

	for (i = 0; i < size; ++i)
		mya[i] = i + 10;

	for (i = 0; i < size; ++i)
		fprintf(stdout, "ans->%d\n", mya[i]);
}

int main(int argc,  char **argv)
{
	myfunc(12);
	return 0;
}

I am trying to use a vla (variable length array) in my code but I see that the array is not initialized properly when I debug the program.
Here's the 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;
}

and here's what I get when debugging :
[img]http://i51.tinypic.com/2ekko5g.png[/img]

Hello, i run your code and doesn't seem to have any problems...i just removed the int declarations from the loop. It isn't acceptable in c to declare your counters inside the loop (as in c++). Try and run the code below:

#include <stdio.h>

      #include <stdbool.h>



      int main (int argc, const char * argv[]) {

      // insert code here...

      int size;
      int rows, 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;
      printf("%d ", myArray[rows][columns]);
      }
     }
      myArray[0][2] = 1;
        return 0;
      }

Or like so if you want to use main's parameters

#include <stdio.h>
#include <stdlib.h>



int main(int argc,  char **argv)
{
	if (argc == 2)
	{
		int i = 0;
		int size = strtol(argv[1], NULL, 10);
		int mya[size];		

		for (i = 0; i < size; ++i)
			mya[i] = 10 + i;

		for (i = 0; i < size; ++i)
			fprintf(stdout, "ans->%d\n", mya[i]);
	}

	return 0;
}

Ok I solved the problem using this code : (I can't see the array elements in debugging mode but it works)

#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");
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.