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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:46:27+00:00 2026-06-18T23:46:27+00:00

void readit(FILE* filePtr, int* num1, int* num2, char** strings, int lines) { int t;

  • 0
 void readit(FILE* filePtr, int* num1, int* num2, char** strings, int lines)
     {
int t;
char line[50];

for (t = 0; t < lines; t++){
    fgets(line, 50, filePtr);
    *(strings + t) = strtok(line, " "));
    *(num2 + t) = atoi(strtok(NULL, " "));
    *(num2 + t) = atoi(strtok(NULL, " "));
}

In this code, the *strings portion is not performing as intended. Every pass through the code overwrites the entire string array. The fgets is reading in the correct data, and every loop through if I print the value of the first token, it is what I expect. Outside of the loop, (or inside if i print (i – 1) all the arrays *str + 1, *str + 2 etc.. Will contain the value of the last pass through.

For example.

         *(strings + 0) = "Hi";
         printf("%s", *(strings + 0)); //Will print hi

         //next iteration
         *(strings + 1) = "You";
         printf("%s", *(strings + 1)); // will print you
         printf("%s", *(strings + 0)); // will print you as well
  • 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-18T23:46:29+00:00Added an answer on June 18, 2026 at 11:46 pm

    The problem is that you only have one string buffer that is constantly being over-written. Remember strtok does not allocate new memory it returns a pointer into the buffer it is passed. Each time through the loop strtok sets *(strings + t) = line. Then in the next loop you over-write the date in line. What you are ending up with is an array of char pointers all pointing to the same string buffer. (You are also returning a pointer to a local variable, which is undefined behavior)

    You code is equivalent to to the following:

        char *strings[2];
        char line[50];
        strcpy(line, "Hi");
        *(strings + 0) = line;
         printf("%s", *(strings + 0)); //Will print hi
    
         //next iteration
         strcpy(line, "You");
         *(strings + 1) = line;
         printf("%s", *(strings + 1)); // will print you
         printf("%s", *(strings + 0)); // will print you as well
    

    So both strings+0 and string+1 point line so of course over-writing line with change what strings+0 prints.

    You either need to allocate memory dynamically or base in a buffer that you can strcpy into. For example:

    char strings[5][50];
    readit(filtPtr, &num1, &num2, strings, 50);
    
    ...
    
    void readit(FILE* filePtr, int* num1, int* num2, char** strings, int lines)
    {
         int t;
         char line[50];
    
        for (t = 0; t < lines; t++){
            fgets(line, 50, filePtr);
            strcpy(*(strings + t), strtok(line, " ")));
            *(num2 + t) = atoi(strtok(NULL, " "));
            *(num2 + t) = atoi(strtok(NULL, " "));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of strings that I'm writing to a file: private void
void test() { unsigned char c; c = (~0)>>1 ; printf(c is %u\n,c); }
- (void)TargetHit:(int)target{ void (^threadBlock)(void) = ^{ NSLog(@respond to selector %d, [self respondsToSelector:@selector(changeImageOfTarget:)]); [[NSOperationQueue mainQueue]
void SaveFiles(out XElement Attachments) { Attachments = null; if (Uploader1.UploadedFiles.Count() > 0) { Attachments
void foo(Node* p[], int size){ _uint64 arr_of_values[_MAX_THREADS]; for (int i=0 ; i < size
void say(char msg[]) { // using pointer to print out the first char of
void AFCQueue::ExtractValuesSecComplex(int startIndex, int endIndex,int helperIndex) { int size = 0,i,index; TimeType min_timestamp; bool
My Text file contains these contents: public class MyC{ public void MyMethod() { System.out.println(My
I have a very strange problem when reading a binary file. void metaDataProcess(FILE *f){
I've got two functions: public void SaveMap() { StreamWriter stream = new StreamWriter(file.txt); for

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.