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 7586447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:25:56+00:00 2026-05-30T19:25:56+00:00

Working on some self study in understanding structures in C. I’ve made a small

  • 0

Working on some self study in understanding structures in C.

I’ve made a small program that gets info from the user and then prints it back via functions.
I used two different methods of handing off the data using a couple examples in the C Primer Plus book.

What happens is that I can input the data but when it prints it back out the numeric data is ok but only the first character in each string is printed with garbage after it.

My code is below for review. I can’t figure out what the issue is.

Any help would be great. Thanks!

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

struct stats {
char name;
char city;
int wins;
int losses;
int draws;
};

void screen_print(char name,char city,int wins,int losses,int draws);
void team_input (struct stats * ptr);

int main()
{
struct stats team;

team_input(&team);
screen_print(team.name,team.city,team.wins,team.losses,team.draws);

return 0;
}

void screen_print(char name,char city,int wins,int losses,int draws)
{
// system("cls");
printf("==================================================\n");
printf("Name:\t\t\t%s\n",&name);
printf("City:\t\t\t%s\n",&city);
printf("Number of Wins:\t\t%d\n",wins);
printf("Number of Losses:\t%d\n",losses);
printf("Number of Draws:\t%d\n",draws);
printf("==================================================");

}
void team_input (struct stats * ptr)
{
system("cls");

printf("Enter Team name: ");
scanf("%s",&(ptr->name));
printf("\nEnter City:");
scanf("%s",&(ptr->city));
printf("\nEnter Wins:");
scanf("%d",&(ptr->wins));
printf("\nEnter Losses:");
scanf("%d",&(ptr->losses));
printf("\nEnter Draws:");
scanf("%d",&(ptr->draws));
}
  • 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-05-30T19:25:57+00:00Added an answer on May 30, 2026 at 7:25 pm

    name and city are single characters only: they are not strings.

    scanf("%s",&(ptr->name)); is invalid and will be overwriting memory as an attempt is being made to read a string into a single char.

    printf("%s", &name); expects name to be a null terminated string, so it will print the name char then random characters until a null is found somewhere in memory.

    Change to:

    struct stats {
        char name[20]; /* Or greater than 20 if required */
        char city[20];
        int wins;
        int losses;
        int draws;
    };
    

    or dynamically allocate memory before populating if maximum possible length of name and city are unknown beforehand.

    Change the printf() statements to:

    printf("Name:\t\t\t%s\n", name);
    printf("City:\t\t\t%s\n", city);
    

    and scanf() statements to:

    scanf("%s",ptr->name);
    scanf("%s",ptr->city);
    

    and screen_print() signature to:

    void screen_print(char* name,char* city,int wins,int losses,int draws)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working on some tweaks for a build script, I noticed that the output from
I have been working on some legacy C++ code that uses variable length structures
I am working with some CSS that is poorly written to say the least.
I'm working on some code that uses the System.Diagnostics.Trace class and I'm wondering how
I'm working with some code that is confusing me and I'm wondering if I'm
I was doing some self-learning on how to pass data from JQuery Ajax to
I'm working on porting some ancient code (10.2 era) from NSCoding/plist based archiving to
Working on some code today, I found that the following would work in 5.3,
I'm working on a program that is going to be used with a Time
I'm having an issue with a small program that I wrote. It does what

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.