2

I would like to create arrays within an array in C without a pre-defined number of characters or input in the array. The following is my code:

{
    int noOfStudents,noOfItems;
    int *grades;
    int i;
    char a[];
    printf("Please enter number of students\n");
    scanf("%d", &noOfStudents);
    printf("Please enter number of items\n");
    scanf("%d", &noOfItems);

    for (i = 0; i < noOfStudents; i++)
    {
        a[i] = (int *)malloc((sizeof(int))*noOfItems);
    }

I've been thrown an error

c(2133): 'a': unknown size

How do I successfully create arrays within an array by means of malloc?

2
  • an array of what? char or int?? use int **a = malloc(sizeof(int *)*nbOfStudents); first. You want a bidimensional array. Commented Nov 15, 2017 at 8:57
  • @Sarah Collins You are here about three years and till now are you so bad?:) Commented Nov 15, 2017 at 10:59

4 Answers 4

2

You can make use of a VLA (Variable length array).

You need to rearrange your code like

int noOfStudents = -1, noOfItems = -1;
int *grades;                                //is it used?
int i;

printf("Please enter number of students\n");
scanf("%d", &noOfStudents);

//fail check

int *a[noOfStudents];             // this needs to be proper.

//VLA

printf("Please enter number of items\n");
scanf("%d", &noOfItems);

//fail check

for (i = 0; i < noOfStudents; i++)
{
    a[i] = malloc(noOfItems * sizeof(a[i]));   //do not cast
}
Sign up to request clarification or add additional context in comments.

2 Comments

You can also use int (*a)[noOfStudents] = malloc(sizeof *a * noOfItems); instead of a VLA of pointers.
@mch no doubt, just another approach.
2

You want a bi-dimensional array, to hold the list of lists of integer items. You can do that by declaring a pointer on integer pointers.

So you want to declare

int **a;

then

printf("Please enter number of students\n");
if (scanf("%d", &noOfStudents)==0 && noOfStudents<=0)  // bonus: small safety
{
    printf("input error\n");
    exit(1);
}
// now we are sure that noOfStudents is strictly positive & properly entered
a = malloc(sizeof(int*)*noOfStudents);

Then you have your array of pointers allocated, and the rest of your code is OK (don't cast the return value of malloc BTW)

A variant is:

a = malloc(sizeof(*a)*noOfStudents);

(so if type of a changes, the size follows, not that it matters here since they're all pointers)

Comments

0

Use pointer instead of array and allocates the memory of that pointer dynamically using malloc or calloc function.

Like this:

int *a;

a = malloc((sizeof(int)*noOfItems);

Comments

-2

You may try the function malloc, which dynamically allocates memory and returns a pointer to it. Then you can cast the pointer to a pointer pointing to an array of a certain type.

3 Comments

Never heard of mallocate
Either update the answer or remove it, it's no good to anyone. To best, it is misleading and not helpful.
sorry, that's a typo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.