Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8413685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:57:56+00:00 2026-06-10T00:57:56+00:00

I am writing a fantasy football draft program for fun. I encountered a strange

  • 0

I am writing a fantasy football draft program for fun.

I encountered a strange problem. I assign a value to a struct field and that happens, but it also assigns that value to another field in the struct. Apologies for the messy debugging printf statements.

I clearly don’t understand something about struct field assignment.

code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define TRUE 1

int QB_count = 0;
int RB_count = 0;
int WR_count = 0;
int TE_count = 0; 
int DEF_count = 0;

struct Player { 
    char *name;
    char *position;
    int age;
    int bye_week;
};

int get_name (struct Player *drafted) {
    char name[20];
    fputs("Enter Player Name: ", stdout);
    fflush(stdout);
    if (fgets(name, sizeof name, stdin) != NULL){
        char *newline = strchr(name, '\n');
        if (newline != NULL){
            *newline = '\0';
        }
        drafted->name = name;
        printf("You've drafted: %s\n", drafted->name);
    }
    return 0;
}

int get_position(struct Player *drafted){
    char position[20];
    int depth;
    char *nametemp = drafted->name;
    printf("nametemp: %s\n", nametemp);
    fputs("Enter Player Position in 'QB/RB/WR/TE/DEF' format: ", stdout);
        fflush(stdout);
        if (fgets(position, sizeof position, stdin) != NULL){
                char *newline = strchr(position, '\n');
                if (newline != NULL){
                        *newline = '\0';
                }
                drafted->position = position;

        if (strcmp(position, "QB") == 0){
            QB_count++;
            depth = QB_count;

        } else if (strcmp(position, "RB") == 0){
                        RB_count++;
                        depth = RB_count;

                } else if (strcmp(position, "WR") == 0){
                WR_count++;
                        depth = WR_count;

                } else if (strcmp(position, "TE") == 0){
                        TE_count++;
                        depth = TE_count;

                } else if (strcmp(position, "DEF") == 0){
                        DEF_count++;
                        depth = DEF_count;

                } else {
            printf("Please re-enter position information using the format 'QB' or 'qb'\n");
            get_position(drafted);
            return 0;
        }
        drafted->name = nametemp;
        printf("NAME: %s\n", drafted->name);
        printf("You've drafted %s at: %s%d\n", drafted->name, drafted->position, depth);
        }
        return 0;

}

int get_age (struct Player *drafted){
    return 0;
}

int get_bye_week (struct Player *drafted){
    return 0;
}

int main (){ 
    int stop = 0;
    char text[20];
    while (TRUE){
        struct Player drafted;
        printf("Welcome to the 2012 Draft Day Program\n");
        get_name (&drafted);
        printf("NAME_MAIN: %s\n", drafted.name);
        get_position(&drafted);
        printf("You've drafted %s at: %s\n", drafted.name, drafted.position);
        get_age(&drafted);
        get_bye_week(&drafted);
        fputs("Would you like to draft another player?\n" 
            "Enter '1' for no, '0' for yes\n", stdout);
        fflush(stdout);
        if(fgets(text, sizeof text, stdin)){
            int number;
            if (sscanf(text, "%d", &number) == 1){
                if (number == 1){
                    printf("Draft Ended!\n");
                    break;
                }       
            }
        }
    }
    return 0;
}

The resulting output is:

Welcome to the 2012 Draft Day Program
Enter Player Name: Aaron Rodgers
You've drafted: Aaron Rodgers
NAME_MAIN: Aaron Rodgers
nametemp: Aaron Rodgers
Enter Player Position in 'QB/RB/WR/TE/DEF' format: QB
NAME: QB
You've drafted QB at: QB1
You've drafted QB at: QB
Would you like to draft another player?
Enter '1' for no, '0' for yes
1
Draft Ended!

Why does drafted.name become “QB”?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:57:57+00:00Added an answer on June 10, 2026 at 12:57 am

    In your get_name function you’re assigning to the name field of your struct Player a stack variable name.

    In this line:

    drafted->name = name;
    

    name is declared in the function and so it’s scope is limited to that function. Once get_name returns, the variable goes out of scope, and attempts to use that memory invoke undefined behavior.

    Instead of using the simple assignment, you need to allocate space for drafted->name using malloc, and use strncpy to make a copy of the name. If strdup is available, you can use that to allocate the space and do the copy in a single step. Alternately, you could allocate space for drafted->name before reading the name, and use it in place of the name variable.

    As a final option, if you assume a maximum length for names – your current code allows names up to a string length of 19 – you can simply declare an array of that size for each struct Player:

    struct Player
    { 
        char name[NAME_MAXLEN];
    

    You have an identical problem with your position field in the get_position function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Writing a program in which I need to split strings from a struct linked
Writing a program for the iphone. Realized that I forgot to release an object,
Writing htaccess that allows me to remove index.php from the URL can confuse search
Writing documentation in html requires some code examples. What to do with characters that
Writing a python program, and I came up with this error while using the
Writing a client application that sends images to a server via a webservice. As
Writing a simple example from Odersky's book resulted in the following problem: // AbstractElement.scala
Writing a sound control applet with GUI that communicates with a device via USB.
Writing an app that will include the ability to decompress zip and rar files.
Writing a SQL query that should return the average charges for a preceding 91

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.