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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:38:36+00:00 2026-06-18T07:38:36+00:00

so I am pretty new to C programming, and am having a problem that

  • 0

so I am pretty new to C programming, and am having a problem that I truly do not understand. I am reading lines from a file, and trying to store each line (as a string) in an array. I am then using strtok on that line to parse another value out of the line. For some reason, the value I put in the array changes after I use strtok. Code is below:

while(fgets(readLine, 100, inFile) != NULL) { 
    printf("j = %d\n", j);
    puts(readLine); 
    machineList[j] = readLine;
    puts(machineList[j]); //the value of machineList[j] at this point is equal
                          // to the current readLine 

    int i=0;

    day = strtok(readLine, " ,");

    puts(machineList[j]); //the value of machineList[j] at this point is no
                          //longer what it was at the previously noted point

    while(i<3) {
        day=strtok(NULL, " ,");
        i++;
    }
    dayList[j]=atoi(day);

    printf("Day is: %d\n\n", dayList[j] );  //these values come out as expected
    j++;

}

Can anyone explain why this is happening? I don’t understand, since its not like I am reassigning machineList[j]=readLine. Thus, even if the value of readLine changes, it shouldn’t change the value in machineList[j]. Again, I am new to C, so I realize my code semantics could be awful- anything is helpful. Thanks!

  • 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-18T07:38:37+00:00Added an answer on June 18, 2026 at 7:38 am

    strtok modifies the original string by replacing the delimiters with null bytes.

    I assume machineList is an array of pointers to char, so this:

    machineList[j] = readLine;
    

    …makes machineList[j] a pointer to readLine. Then this:

    day = strtok(readLine, " ,");
    

    …modifies readLine, which also modifies machineList[j] since it points to it. You’ll want to make a copy instead:

    // allocate machineList[j]
    strcpy(machineList[j], readLine);
    

    If you don’t make a copy, when your while loop ends, machineList will basically be an array of dangling pointers.

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

Sidebar

Related Questions

Pretty new to C# but not new in programming in generally. I'm still trying
I'm pretty new to web programming, reading a book on ASP.NET, and I notice
I'm pretty new to Python programming and would appreciate some help to a problem
I am pretty new to GUI programming and to Qt itself, albeit having a
I'm pretty new to java (and programming in general), and am having a tough
I'm pretty new to programming and made a program to fetch inventory data from
I am pretty new to programming and definitely to Zend/Lucene indexing. From what I
Hey there, I'm pretty new to programming and I've got a problem with the
I'm pretty new to programming and am having some trouble understanding the output of
Pretty new to Java programming, but I'm having trouble getting this to run. I

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.