I'm new to c and don't really understand how 2d arrays work. When the code is executed it prompts for me to enter a course and then when the loop is broken the print statement doesn't show what I inputted.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char courses[60][100];
char ans ='y';
int i=0;
int p,j;
while (ans == 'y'){
printf("Enter course:\n");
for (p=0;p<j;p++)
scanf("%s", &courses[p]);
getchar();
i++;
printf("Would you like to enter another course? (y or n) \n");
ans = getchar();
}
printf("courses are %s",courses[p]);
}
jdoes and is currently not set or manipulated so you’ll likely run into storage corruption.printf("courses are %s",courses[p])outputs one string, but the code does not enter any data forcourses[p], only up tocourses[p-1].jvalue, the lastprintfis also wrong.pis not a valid index at that point and if you want to print out all the courses you need anotherforloop to print each of thecoursesstrings.