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

  • Home
  • SEARCH
  • 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 8680541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:16:40+00:00 2026-06-12T21:16:40+00:00

I know how to read a file into a 2d array using for loops

  • 0

I know how to read a file into a 2d array using for loops to direct where you are storing the data in C, but what I am interested in is if you can use one fread() call to do this. That is, can I do something like this:

int A[5][5];
fread(&A, sizeof(int), 25, filePtr);

I am getting seg faults when I tried this so right now so I am wonder if this can be done at all.

EDIT:
Alright, I guess using fread this way isn’t really my problem. My problem is that I have to call fread from within a function and A is defined outside of that function.

func(int size, int ***A)
{
  fread(*A, sizeof(int), size*size, filePtr);
}

main
{
  int A[n][n];
  func(n, &A);
}

So my problem is that my call to fread still seg faults and I HAVE to use a triple pointer for my function prototype. I tried just A (instead of *A) for fun but still got the same seg fault. It looks like I’m just overlooking something with pointers.

  • 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-12T21:16:41+00:00Added an answer on June 12, 2026 at 9:16 pm

    My problem was that of contiguous memory. It’s actually very straight forward when you realize that fread is only set up to take single pointers. Therefore it can only take 1-d arrays. However, if your 2-d array is represented in memory contiguously it is analogous to a 1-d array. Therefore, just pass fread a pointer to the first element in your 2-d array, it will start looping through it as if it is a 1-d array and all will be well.

    So just make sure your 2-d array is contiguously assigned and you should be fine.

    malloc(n*n *sizeof(int)) 
    

    instead of mallocing in a for loop for each row.

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

Sidebar

Related Questions

I've read a file into an array of characters using fread. Now I want
I need to read a file in PHP, but I only know the end
I have a data file, which I need to read. I know to read
I know I can read off /proc/[id]/maps but I'm not sure if the first
How does one access data imported from a CSV file by using dynamic note
I am trying to load a CSV file into an array using ColdFusion (version
I would like to know how is possible to read a file placed in
Anyone know how to read Outlook nickname list file (.NK2)? Any document on how
Does anyone know how to read a x.properties file in Maven. I know there
I know how to read files in PHP but I want to read the

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.