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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:45:22+00:00 2026-06-14T15:45:22+00:00

I have this struct: struct match { int round; int day, month, year; int

  • 0

I have this struct:

struct match {
    int round;
    int day, month, year;
    int hour, minutes;
    char *home_team;
    char *visitor_team;
    int home_score;
    int visitor_score;
    int number_of_spectators;
};

And i have this function that loads in som values from a file.

struct match matches[198];

int get_matches_from_file(struct match *matches)

And i set the values with this in a for loop:

int year, month, day, hour, minute;
int m_round;
int home_score, visitor_score;
char home[3], visitor[3];
int spectators;

sscanf(line[i], "%d %d.%d.%d kl.%d.%d %s - %s %d - %d %d", &m_round, &day, &month, &year, &hour, &minute, home, visitor, &home_score, &visitor_score, &spectators);

matches[i].round = m_round;
matches[i].day = day;
matches[i].month = month;
matches[i].year = year;
matches[i].hour = hour;
matches[i].minutes = minute;
matches[i].home_team = home;
matches[i].visitor_team = visitor;
matches[i].home_score = home_score;
matches[i].visitor_score = visitor_score;
matches[i].number_of_spectators = spectators;

But when i print out the structs. All home_team and visitor_team strings are the same as the last ones in the file i load in. As if they were all changed in the end of the loop.

This is an example of the last line in the line[] array

33 23.05.2012 kl. 20.00 AGF - FCM 0 - 2 12.139

All home_team and visitor_team gets set to AGF and FCM

  • 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-14T15:45:24+00:00Added an answer on June 14, 2026 at 3:45 pm

    You have only allocated a single char for home_team and visitor_team. Use char arrays in your struct to provide space for a string:

    #define MAX_NAME_BYTES(32) /* include space for nul terminator */
    struct match {
        int round;
        int day, month, year;
        int hour, minutes;
        char home_team[MAX_NAME_BYTES];
        char visitor_team[MAX_NAME_BYTES];
        int home_score;
        int visitor_score;
        int number_of_spectators;
    };
    

    then use strcpy to copy the results into the struct:

    strcpy(matches[i].home_team, home);
    strcpy(matches[i].visitor_team, visitor);
    

    Alternatively, use char pointers in your struct (as you now do in your edited question) and allocate them using strdup:

    matches[i].home_team = strdup(home);
    matches[i].visitor_team = strdup(visitor);
    

    Note that you’ll need free these strings when you discard the struct:

    free(matches[i].home_team);
    free(matches[i].visitor_team);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code struct Student { char name[48]; float grade; int marks[10,5]; char
I have this struct; #define BUFSIZE 10 struct shared_data { pthread_mutex_t th_mutex_queue; int count;
I have this type which is basically a struct { int x,y,z; } that
Ok... I have this struct and comparison function- struct Edge { char point1; char
I currently have a class like this: struct Rgb { static const int NUM_CHANNELS
I have this code: struct A{}; template<class T = A> struct B { void
I have something like this: struct D { virtual void operator() {...}; } struct
I have this function in C++: struct MyObject { } testAlgorithm(array<String^>^ algorithms, MyObject^ myObject)
I have a structure like this struct structure { BaseObject &A; //BaseObject has a
I have a struct that's defined like this: struct Vec3 { float x, y,

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.