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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:25:52+00:00 2026-06-18T16:25:52+00:00

I have this D-code for reading data from a file: int foo (SomeStruct[] somestruct_array)

  • 0

I have this D-code for reading data from a file:

int foo (SomeStruct[] somestruct_array)
{
  // get file_handle...
  for (uint i = 0; i < 1000; i++) {
    file_handle.readf ("%e %e %e %e %e %e\n",
            &(somestruct_array[i].pos_x), &(somestruct_array[i].pos_y),
            &(somestruct_array[i].pos_z), &(somestruct_array[i].vel_x),
            &(somestruct_array[i].vel_y), &(somestruct_array[i].vel_z));
  }
  return 0;
}

As you can see, the lines are getting quiet long and I keep repeating somestruct_array[i]. It’s not a big deal, but I tried to shorten that and write:

int foo (SomeStruct[] somestruct_array)
{
  // get file_handle...
  for (uint i = 0; i < 1000; i++) {
    auto elem = somestruct_array[i];
    file_handle.readf ("%e %e %e %e %e %e\n",
            &(elem.pos_x), &(elem.pos_y),
            &(elem.pos_z), &(elem.vel_x),
            &(elem.vel_y), &(elem.vel_z));
  }
  return 0;
}

This results in all the element members being nan.

So: Is it possible to get a reference to array elements?

  • 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-18T16:25:53+00:00Added an answer on June 18, 2026 at 4:25 pm

    The problem is that elem is a copy of the array element, so the pointers you pass to readf are pointers to the temporary value.

    You have two options here, you can use a pointer:

    for (uint i = 0; i < 1000; i++) {
        auto elem = &somestruct_array[i];
        file_handle.readf ("%e %e %e %e %e %e\n",
            &(elem.pos_x), &(elem.pos_y),
            &(elem.pos_z), &(elem.vel_x),
            &(elem.vel_y), &(elem.vel_z));
    }
    

    or, you can iterate the array by reference:

    foreach (ref elem; somestruct_array[0..1000]) {
        file_handle.readf ("%e %e %e %e %e %e\n",
            &(elem.pos_x), &(elem.pos_y),
            &(elem.pos_z), &(elem.vel_x),
            &(elem.vel_y), &(elem.vel_z));
    }
    

    Note that if the array is 1000 elements long, you can just write:

    foreach (ref elem; somestruct_array)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using C to open a file for reading. I have this code
I'm having problems reading lines from a text file in PHP. I have this
I have posted the following code where I am reading from an input file
I have this simple code where I am sending http request and reading all
I have a python ndarray temp in some code I'm reading that suffers this:
Reading existing code at work, I wondered how come this could work. I have
I have this code : void Main() { System.Timers.Timer t = new System.Timers.Timer (1000);
I have a program reading from a text file (currently 653 lines long) all
I have an application that loads data from a .csv file and shows it
I have a piece of code that looks like this: downloadsByExtensionCount = defaultdict(int) downloadsByExtensionList

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.