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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:39:43+00:00 2026-06-18T04:39:43+00:00

So in my program there’s a struct: typedef struct Point { double x, y;

  • 0

So in my program there’s a struct:

typedef struct Point {
    double x, y;
    char *label;
} point;

And then I read some information from a file and assign various labels to various point structs within an array. The problem is that although the x, y values are assigned properly, the label is the same for each struct in memory.

point set[3];

FILE *file = fopen(argv[1], "r");

int count = 0;
char *pch;
char line[128];

if(file != NULL){
    while(fgets (line, sizeof line, file) != NULL){
        pch = strtok(line, " ");
        int i = 0;

        while(pch != NULL){
            if(i==0){set[count].label = pch;}
            if(i==1){set[count].x = atof(pch);}
            if(i==2){set[count].y = atof(pch);}
            pch = strtok(NULL, " ");
            i++;
        }
        count++;
    }
    fclose(file);
}

//these values should not be the same, though they are
printf("%s\n", set[0].label); 
printf("%s\n", set[1].label);

Is there some sort of workaround that would allow my struct to remain the same and yet assign values properly?

  • 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-18T04:39:44+00:00Added an answer on June 18, 2026 at 4:39 am

    You need to assign memory for each label instance. Either as an array

    typedef struct Point {
        double x, y;
        char label[50];
    } point;
    
    strcpy(set[count].label, pch);
    

    or by dynamically allocating memory for each label instance

    set[count].label = malloc(strlen(pch)+1);
    strcpy(label, pch);
    

    (Make sure to later free(set[count].label) in the latter case)

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

Sidebar

Related Questions

I have a program, and in that program there is some variables (username and
I have a program where there is a master/slave setup, and I have some
Is there any difference between starting a program file with any of those? #!/usr/bin/env
is there any small working program for recieving from and sending data to client
This program is supposed to open a txt file, record the answers from two
Possible Duplicate: Run console application from other console app In my program there is
I am having some issue with understanding of Core Data. In my program there
How can I close my C# WinForms program while there are some controls like
I am currently working on a VB.Net program where there are two parallel multiline
Is there a program which I can run like this: py2py.py < orig.py >

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.