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.

user912447
  • 696
  • 1
  • 11
  • 31

3 Answers3

2

A is already a pointer to the array. &A is a pointer to this pointer, which is not what you want. All you need to do is replace &A with A.

epsalon
  • 2,294
  • 12
  • 19
  • Yeah, you are definitely right. Looks like my problem is with passing A to a function as it turns out, not with the call to fread. – user912447 Oct 04 '12 at 01:14
1

The best way to think of arrays in C is synonymous with pointers except on one front: you can't change the address they reference like a regular pointer. For example:

int A[5][5];
int *aptr = A;  // notice no &

aptr += 2; // works fine.
A += 2;    // does NOT compile.

Similarly this...

int A[5][5];
fread(A, sizeof(int), 25, filePtr); // again, note no &

and this...

int A[5][5];
int *aptr = A;
fread(aptr, sizeof(int), 25, filePtr); // no & here either.

both end up sending the same address to fread().

Again, in C, think of arrays as pointers who's "value" (the memory address) you cannot change. All other aspects of them are virtually the same. It will save you a LOT of headache later on.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • Interestingly enough, I do grasp this bit of pointer meaning. It is when pointers *appear* to not behave like that when I get confused. Right now I'm trying to correct my thinking so they no longer appear that way. – user912447 Oct 05 '12 at 22:44
  • Work with it a ton more, it will gel. – WhozCraig Oct 05 '12 at 23:33
0

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.

user912447
  • 696
  • 1
  • 11
  • 31