Can i change the array's rows length after i define it as constant or any other way?
Can you under my code or explanation?


#define CLASS_SIZE 20 //First define the array rows's length

void main()
	double gpa, average, highest, lowest;
	int student, rows, i, j;
	char grade[CLASS_SIZE][5]; //Wish to change the CLASS_SIZE

	printf("Enter the number of student : ");
	scanf("%d", &CLASS_SIZE); //Can i change the arrays length after declared ?

	for(j=0;j<CLASS_SIZE;j++)   //What should i modify?
		printf("\nStudent No.%d :\n", j+1);

			printf("Enter subject %d's grade : ", i+1);
			scanf("%c", &grade[j][i]);

	printf("|           |             SUBJECT         |\n");
	printf("|  STUDENT  +=============================+\n");
	printf("|           |  1  |  2  |  3  |  4  |  5  |\n");
		printf("|     %d     |", j+1);
			printf("  %c  |", grade[rows][i]);

There is realloc(), but it's not available for declared arrays, just allocated ones. But you can malloc or calloc a new array, and then transfer the old, into the new array, yourself. Doing it yourself, gives you a better chance at understanding the process - good for a student. No doubt realloc() is handy, of course, so maybe you want to start out with an allocated array, instead of a declared one?

From the Pelles C help file:

Changes the size of an allocated object.

void * realloc(void *ptr, size_t size);

Declared in:

The realloc function deallocates the old object pointed to by ptr and returns a pointer to a new object that has the size specified by size. The contents of the new object shall be the same as that of the old object prior to deallocation, up to the lesser of the new and old sizes. Any bytes in the new object beyond the size of the old object have indeterminate values.

This article has been dead for over six months. Start a new discussion instead.