Thread: Variable Array Size

  1. #1
    Registered User
    Join Date
    May 2010
    Location
    Isla Vista
    Posts
    34

    Variable Array Size

    In this code, I scanf to get the NxN array size. Then I try to create an array of that size, then make it a zero matrix. Any clues?
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main (){
    	/* Create a matrix of NxN dimensions */
    	int i, j, k;
    	printf("\nWhat NxN size of array do you want?\n");
    	printf("N=");
    	scanf("%d",&k);
    	getchar();
    	printf("You desire a %dx%d Matrix.\n" ,k,k);
    	/* Create a NxN matrix */
    	/* Array is now an NxN matrix. */
    	/*					   */
    	/*      Display the matrix     */
    	
    	int M[k][k];
    	for (i=0; i<k; i++){
    		for (j=0;j<k;j++)
    			M[i][j]=0;
    	}
    	printf("\nHere is a zero array with the specified dimension %dx%d." , k,k);
    	for (i=0; i<k; i++){
    		for (j=0;j<k;j++)
    			printf("%f", M[i][j]);
    		printf("\n");
    	}
    	getchar();
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work? In case you did not know, the variable length array language feature is relatively new, having been introduced to standard C in the 1999 edition of the C standard.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Isla Vista
    Posts
    34

    Errors

    This is the error report from Microsoft Visual C++ 2010 Express.
    Code:
    1>c:\users\bryce\documents\visual studio 2010\projects\taylorsrsexpmatrix\taylorsrsexpmatrix\expmatrix.cpp(13): error C2057: expected constant expression
    1>c:\users\bryce\documents\visual studio 2010\projects\taylorsrsexpmatrix\taylorsrsexpmatrix\expmatrix.cpp(13): error C2466: cannot allocate an array of constant size 0
    1>c:\users\bryce\documents\visual studio 2010\projects\taylorsrsexpmatrix\taylorsrsexpmatrix\expmatrix.cpp(13): error C2057: expected constant expression
    1>c:\users\bryce\documents\visual studio 2010\projects\taylorsrsexpmatrix\taylorsrsexpmatrix\expmatrix.cpp(13): error C2466: cannot allocate an array of constant size 0
    1>c:\users\bryce\documents\visual studio 2010\projects\taylorsrsexpmatrix\taylorsrsexpmatrix\expmatrix.cpp(13): error C2087: 'M' : missing subscript
    1>c:\users\bryce\documents\visual studio 2010\projects\taylorsrsexpmatrix\taylorsrsexpmatrix\expmatrix.cpp(13): error C2133: 'M' : unknown size
    It won't even compile. I agree that it should run also.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, I notice the ".cpp" file extension, which means that you are very likely compiling as C++, and C++ does not have a variable length array language feature (although it may be supported as a compiler extension). But even if you are compiling as C, if I remember correctly, MSVC does not support this particular C language feature (hence my mention that it is relatively new, from C99).

    You may want to consider the use of the likes of malloc(), realloc() and free() from <stdlib.h>. Alternatively, if the upper bound of N is small enough, you can simply create a fixed size 2D array of that largest possible size, but only use a portion of it as needed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Isla Vista
    Posts
    34
    Thank you for the quick replies!!

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Isla Vista
    Posts
    34
    I am not aware of the malloc, realloc or free functions. If this is a way to create my variable size array then I'm going to need to learn those commands.

  7. #7
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    You do certainly, as they are one of the most useful - if not the most useful - functions out there. And are very closely related to pointers, which are a very important concept in C.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    27
    Use double pointer to int (int** pMatrix) to solve your problem. You have to create array and then array on each item of array.

    I hope you will write it, hint is enough.

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Isla Vista
    Posts
    34
    Is that before the
    Code:
    int M[k][k];

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    269
    Quote Originally Posted by bchudomelka View Post
    I am not aware of the malloc, realloc or free functions. If this is a way to create my variable size array then I'm going to need to learn those commands.
    You can do this real easily with malloc

    Here's a goodT overview of how pointers, arrays, and malloc work

    http://c-faq.com/aryptr/dynmuldimary.html

    Code:
    	#include <stdlib.h>
    
            int nrows = 10;
            int ncolumns = 20;
     
            // create a [10][20] array
    	int **array1 = malloc(nrows * sizeof(int *));
    	for(i = 0; i < nrows; i++)
    		array1[i] = malloc(ncolumns * sizeof(int));
    then you can index the elements like array[3][4] for example
    Last edited by dayalsoap; 05-19-2010 at 09:15 AM.

  11. #11
    Registered User
    Join Date
    May 2010
    Location
    Isla Vista
    Posts
    34
    So, the int **array1 assigns the array1 pointer to be of int type? malloc allocates the memory of the nrows in the 1st instance? the for loop causes array element i to be allocate the memory for ncolumns?

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bchudomelka View Post
    So, the int **array1 assigns the array1 pointer to be of int type? malloc allocates the memory of the nrows in the 1st instance? the for loop causes array element i to be allocate the memory for ncolumns?
    **array1 indicates a pointer to a pointer (**), which is why the malloc looks like malloc(nrows * sizeof(int *)). This is space for an array, so array1 is a pointer to an array of pointers. Each element of the array is an int pointer which has space for an array of ints allocated to it -- malloc(ncolumns * sizeof(int)). So this is an array of "row pointers" to rows of columns of ints.

    I'd recommend you try a 1D array first, which would be a single row (*array).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    269
    Quote Originally Posted by MK27 View Post
    **array1 indicates a pointer to a pointer (**), which is why the malloc looks like malloc(nrows * sizeof(int *)). This is space for an array, so array1 is a pointer to an array of pointers. Each element of the array is an int pointer which has space for an array of ints allocated to it -- malloc(ncolumns * sizeof(int)). So this is an array of "row pointers" to rows of columns of ints.

    I'd recommend you try a 1D array first, which would be a single row (*array).
    This is correct.

    The website I gave gives a pretty good image
    http://c-faq.com/aryptr/array1.gif

    Try a 1d array first

    Code:
    int col = 10;
    int *array1d;
    array1d = (int*)malloc(sizeof(int) * col);
    This will create a 1d array of size 10...

  14. #14
    Registered User
    Join Date
    May 2010
    Location
    Isla Vista
    Posts
    34
    Thanks so much. I was able to create the malloc. It's starting to make more sense.

  15. #15
    Registered User
    Join Date
    May 2010
    Location
    Isla Vista
    Posts
    34
    This is an example I create with the code you gave.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     int main(){
    	 int k;
    	 int col = 10;
    	int row = 10;
    	int *array1d;
    	array1d = (int*)malloc(sizeof(int) * col*row);
    	printf("Which element do you want to see?\n");
    	printf("Element:");
    	scanf("%d", &k);
    	printf("The first element of array1d:%d\n", array1d[k]=k);
    	scanf("%d", &k);
    	if (k==1) {main();}
    	return 0;
     }
    How does that expand to 2x2?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM