0

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);
    }
}
4
  • The first sensible C line within the i loop (you can remove the first 3 statements) is scanf("%s", idinfo[i].Name); (actually should be idinfo[i].name) but you have not allocated any memory - the struct contains only a pointer. Commented Mar 5, 2019 at 15:51
  • In order to be able to use get_string don't you need to include <cs50.h>? Commented Mar 5, 2019 at 16:02
  • You also mix the input methods, and it is possible that get_string will immediately return an empty string, due the the newline left in the buffer after the previous scanf. Commented Mar 5, 2019 at 16:04
  • BTW, you write "without using the header". Which header? The cs50.h header filer? Please edit your question and clarify. Commented Mar 5, 2019 at 16:13

3 Answers 3

3

Assuming you do not want to use the cs50 magic, you will have to allocate and deallocate strings. From your code I will also assume that the input strings are supposed to be shorter than 20 characters. The following code does not even ensure that it is true but will not crash either but will simply give unexpected results.

Code could be:

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++)
    {
        // allocate memory for idinfo[i] pointers:
        idinfo[i].name = malloc(20);
        scanf("%19s", idinfo[i].name);
        idinfo[i].dorm = malloc(20);
        scanf("%19s", idinfo[i].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);
    }
    // free allocated memory
    for (int i = 0; i < enrollment; i++)
    {
        free(idinfo[i].name);
        free(idinfo[i].dorm);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You probably want something like this:

#include <cs50.h>    // needed for get_xxx functions and other cs50 stuff
...
// Prompt for students' names and dorms
for (int i = 0; i < enrollment; i++)
{
   idinfo[i].name = get_string("Name: ");
   idinfo[i].dorm = get_string("Dorm: ");
}

But this should be covered in the documentation you were given.

My advice is not using scanf at all but using only the cs50 get_xxx input methods.

And beware: the cs50 string is not a real "string" type but merely the same thing as char *.

Comments

0

Thanks to Serge, I was able to get my code compiling, which is below.

#include <stdio.h>
#include <string.h>
#include <stdlib.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++)
    {
    // allocate memory for idinfo[i] pointers:
        idinfo[i].name = malloc(20);
        idinfo[i].dorm = malloc(20);

        printf("Enter student %d's Name ", i+1); 
        scanf("%19s", idinfo[i].name);

        printf("Enter student %d's dorm ", i+1);
        scanf("%19s", idinfo[i].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);
    }
    // free allocated memory
    for (int i = 0; i < enrollment; i++)
    {
        free(idinfo[i].name);
        free(idinfo[i].dorm);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.