Creating new variable on the go in C

Hi,

So i have this question in C..

this is the scenario:

I need to accept the user's input asking how many subjects, and she enters (say) 6..

Next i store that in a variable (say) 'nos' (number of subjects) and loop that in a for loops, that prints this "Enter subject 1... thru (nos)" which is the user's input.. Now, is there a way i can actually store that in a variable created dynamically? like, when "Enter subject 1: " is displayed, the user must be asked to actually enter the subject, and that must be stored in a variable say s1.. upto snos

this is the code i've got:

{
int a, b, c, d, nos, i;
char var;


printf("Enter the number of subjects: ");
scanf ("%d", &nos);

for (i=1; i<=nos;i++)
{
printf("Subject %d \n:", i);
}
.
.
.
}

So, here's where i would need to know if i can have the subject 1 thru subject (nos) displayed, and after each time it is displayed, the user is allowed to input a TEXT, and stored in a variable created dynamically. I am hoping i am clear :(

Best,
Nikhil
It sounds like you want a dynamic array to store each piece of data.

Look up dynamic memory in C; specifically, malloc() and free() should do what you want.
http://www.cplusplus.com/reference/cstdlib/malloc/
http://www.cplusplus.com/reference/cstdlib/free/
Topic archived. No new replies allowed.