I have been reviewing videos in Walkthrough section from week 2 and decided to implement ages example. After I've implemented it I decide to extend it so program will prompt user fro name and display it. Here goes the code:
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int n = 0;
int s = 0;
do
{
printf("Please enter number of people in a room: ");
n = GetInt();
}
while (n < 1);
int ages[n];
char* names[s];
for (int i = 0; i < n; i++)
{
printf("Please enter name of person #%d: ", i+1);
names[i] = GetString();
}
for (int i = 0; i < n; i++)
{
printf("Please enter age of %s: ", names[i]);
ages[i] = GetInt();
}
printf("Time passes...\n");
for (int i = 0; i < n; i++)
{
printf("A year from now, age of %s will be %d\n", names[i], ages[i] + 1);
}
}
However after certain point it throws out Segmentation Fault (core dumped) error. I have found out that names[i]
in the last for loop is a cause of the problem, when it is gone everything works fine.Can you point out what is the wrong with it? Your help is very much appreciated!