I just took the second lesson of second week, we were taught how to do dynamic integer array, some confusions i need to clear up is when we have initialized an integer and even take the value from user won't it become static for program ? I mean take value from user at run time is dynamic ? Can't we do a program which itself acknowledge how many values it needs ? What we do if the user itself doesn't know how much students are coming in the class, what if user enter i'll enter 3 ages for the students and then there is another one?

int n;

    printf("No of people in room: ");
    n = GetInt(); //we'll get the value from user

int ages[size];
printf("Age of a person: ", i + 1);
for(int i=0; i<n; i++)
    printf("A year from now person %i will be %i year old, i+ 1",ages[ i ]);

What you want to say, is that the array is immutable, which means once you have declared their length, you can't change it to add more values. A possible solution to the problem you describe are linked lists, which you will see later in the course. Or when a new student arrives, but your array is full, you can create a new array, with a bigger length, copy all the values you have stored on the first array, and then add the new student.

  • got it, thanks for the reply. One last thing is this the only way? – Habib ur Rehman Jul 19 '15 at 15:48
  • In programming there is ALWAYS another way. I think those are the simplest ones that come to my mind right now. – ChrisG Jul 19 '15 at 15:50
  • right, thanks again for increasing my knowledge i'll take next lectures of the course now. – Habib ur Rehman Jul 19 '15 at 15:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .