I am just learning arrays and I am experimenting. This one simply won’t work. I am trying to have the user define (1) how many elements an int array can hold and (2) what the value of each elements is. The program now outputs random integers. Maybe this isn’t even possible, but I’m a complete beginner just playing around for the fun of it. XD
#include <stdio.h>
int main(void) {
int scope,elementnr,i,elementvalue,j;
scope = elementnr = i = elementvalue = j = 0;
int array[j];
printf("How many elements should your ARRAY hold: ");
scanf("%d", &scope);
getchar();
for (i = 0; i < scope; ++elementnr, ++i) {
printf("Enter the value of element number %d: ", elementnr + 1);
scanf("%d", &elementvalue);
getchar;
j = elementvalue;
}
printf("You have created an ARRAY that holds %d elements and you have assigned the following values:\n", scope);
for (i = 0; i < scope; ++i) printf("%d\n",array[j]);
}
As an amateur (I haven’t coded pro in 30+ years), I’d say this is a job for malloc(). The memory dynamically allocated from user input can then be used as an array.
Thanks! I’ll look that up!
Wonderful day!
I am sorry, but considering your quite exquisite name, it feels like you searched for Communities already!
Yet, feeling your appreciated interest in the marvelous programming language, have you checked out more focused Communities?:
- [email protected]
- [email protected]
- [email protected]I’d definitely recommend the following book, too!:
- https://amazon.com/dp/0131103628 (“C Programming Language, 2nd Edition”… by Brian W. Kernighan and Dennis M. Ritchie…)Thank you for the recommendations!
Also, it’s never to early to learn about sanitizing user input. For example, checking if scope is neither too big or too small. See e.g. https://stackoverflow.com/a/14099507




