I'm a Noob, struggling with C. I'm having a hard time wrapping my head around pointers, arrow -> and dot . notation. I am working on a very simple program that has 1 struct, but I am unsure of how to get users to input strings without using the <cs50.h> header (which I need to understand for the harder problem I am also trying to wrap my head around).
//#include <cs50.h>
#include <stdio.h>
#include <string.h>
// create a struct, call it pupils
typedef struct
{
char *name;
char *dorm;
}
pupils;
int main(void)
{
// Allocate space for students - generally dynamic, here static
int enrollment = 2;
// create variable 'idinfo', which contains (enrollment) number of structs of type pupils
pupils idinfo[enrollment];
// Prompt for students' names and dorms
for (int i = 0; i < enrollment; i++)
{
// Not the way to do this given various error codes.....
char idinfo[i].Name[20];
char Dorm[20];
// gets idinfo[i].Name
scanf("%s", idinfo[i].Name);
//idinfo[i].name = printf("%s", Name);
// Below syntax works fine when <string.h> header included
idinfo[i].dorm = get_string("Dorm: ");
}
// Print students' names and dorms
for (int i = 0; i < enrollment; i++)
{
printf("%s is in %s.\n", idinfo[i].name, idinfo[i].dorm);
}
}
iloop (you can remove the first 3 statements) isscanf("%s", idinfo[i].Name);(actually should beidinfo[i].name)but you have not allocated any memory - thestructcontains only a pointer.get_stringdon't you need to include<cs50.h>?get_stringwill immediately return an empty string, due the the newline left in the buffer after the previousscanf.cs50.hheader filer? Please edit your question and clarify.