-3

I forgot that an array can't be regularly initialized with a variable in C and during a test instead of initializing the array using malloc and passing It the variable I have initialized it that way - int arr[size] when size is an int variable that I have calculated earlier in the code. I Want to appeal and I would like to know if there is some way to sugar coat it or give a semi explanation on how it could work somehow and that I simply forgot something small (like making the variable a const) instead of making the gross error of allocating the array statically versus dynamically.

scifie
  • 65
  • 1
  • 2
  • 14
  • 2
    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. – too honest for this site Aug 03 '15 at 23:39
  • Arrays can be initialized with variables in C (so long as the variable value, after conversion to int, is positive) . – M.M Aug 03 '15 at 23:41
  • You appear to be talking about VLAs (variable-length arrays), but it's not clear exactly what you're asking. – paddy Aug 03 '15 at 23:41
  • Most compilers support variable length arrays... which compiler are you using? Try upgrading to a recent version of clang or gcc. – James M Aug 03 '15 at 23:54

1 Answers1

-1

Is this what you're asking?

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

int *new_int_array(size_t size, int initialValue)
{
    int i;
    int *data;
    // Allocate the needed memory on the heap.
    data = (int *)malloc(size * sizeof(int));
    if (!data) { return NULL; }
    for (i = 0; i < size; ++i)
    {
        data[i] = initialValue;
    }
    return data;
}

void delete_int_array(int *array)
{
    // Free the array we dynamically allocated
    free(array);
}

int main()
{
    int i;
    int *data;
    size_t arraysize;
    arraysize = 2;
    arraysize += 2;
    data = new_int_array(arraysize, 42);
    for (i = 0; i < arraysize; ++i)
    {
        printf("%d, ", data[i]);
    }
    return 0;
}

This program will print 42, 42, 42, 42,.

Alyssa Haroldsen
  • 3,652
  • 1
  • 20
  • 35
  • I wanted help to STATICALLY define an array with a size that's determined by a variable calculated at run time - so without using malloc. As I have explained I have during a test answered one of the questions that requiring defining an array at runtime done this by calculating during runtime an int variable named mySize and defined an array like that - int myArr[mySize]. Which is a mistake because I should have done it using malloc. Obviously my grade was lowered for that part because this is a complete mistake and a dynamic array can't be defined that way. – scifie Aug 04 '15 at 13:30
  • I Want to appeal the grade deduction so not so many point are lowered for that mistake and I would like to know if there is some way to sugar coat it or give a semi explanation on how it could work somehow and that I simply forgot something small (like making the variable a const) instead of making the gross error of incorrectly allocating a dynamic array. – scifie Aug 04 '15 at 13:32
  • Nope. Dynamically allocated arrays use `malloc` in C. The compiler has to know the size of the stack frame (local variables) at compile time. – Alyssa Haroldsen Aug 04 '15 at 20:07