By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,233 Members | 1,880 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,233 IT Pros & Developers. It's quick & easy.

pointer to typedef 2D array

P: 5
Hello Sir,
In my code I have made a typedef 2D array (large array)

typedef char matrix[matrix_size][matrix_size];

Then in the program I used malloc to allocate memory for the array

matrix *m;
m =malloc(sizeof *m); //Actually i'm using a code from one of your posting

Now in my program I want to use fread to read from a file, a size of one row of this matrix how to do that??

I tried the following:

nread = fread(*(*(m+row)), 1, Row_size, pfile);
where "row" is a for loop variable; I'm looping through the file reading one row at a time

Any ideas how to do that???
Thanks a lot in advance.
mshaaban
Jun 7 '07 #1
Share this Question
Share on Google+
7 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
You have several issues here:

This code does not work:
Expand|Select|Wrap|Line Numbers
  1. matrix *m;
  2. m =malloc(sizeof *m); //Actually i'm using a code from one of your posting
  3.  
m is a pointer to a matrix. The sizeof a pointer is 4. You are allocating 4 bytes. Not good.

matric is defined as:
Expand|Select|Wrap|Line Numbers
  1. char matrix[matrix_size][matrix_size];
  2.  
This array has matrix_size times matrix_size number of elements. So your malloc shoulde be:

Expand|Select|Wrap|Line Numbers
  1. matrix* m = (matrix*)malloc(sizeof(char) * (matrix_size * matrix_size));
  2.  
Now as to reading the array contents from the disc, I'll start by saying there are no 2D arrays in the disc file. You just have a string of chars. Maybe:

a b c d e f

Now if you had a one-dimensional array, this would be easy.:
Expand|Select|Wrap|Line Numbers
  1. char arr[6];
  2. fread( (void*)arr, 1, 6), pfile);   
  3.  
Now comes the magic sauce: There are no 2D arrays in C or C++. There are only one-dimensional arrays. That is this array:
Expand|Select|Wrap|Line Numbers
  1. char arr[6] = {'a','b','c','d','e','f'};
  2.  
has a memory layout of

a b c d e f

whereas this array:
Expand|Select|Wrap|Line Numbers
  1. char arr[3][2] = {'a','b','c','d','e','f'};
  2.  
has a memory layout of

a b c d e f

Kinda the same, right?

It is the first index that defines the number of elements. The other indexes just describe the individual elements.

So all you need to do is read from the disc file to the 2D array as though it were a one-dimensional array starting at the address of arr[0][0]. After you read, you can then use the array as a 2D array.

Expand|Select|Wrap|Line Numbers
  1. char arr[3][2];
  2. fread( (void*)&arr[0][0], 1, 6), pfile);   
  3.  
Jun 8 '07 #2

Expert 10K+
P: 11,448
You have several issues here:

This code does not work:
Expand|Select|Wrap|Line Numbers
  1. matrix *m;
  2. m =malloc(sizeof *m); //Actually i'm using a code from one of your posting
  3.  
m is a pointer to a matrix. The sizeof a pointer is 4. You are allocating 4 bytes. Not good.
Yes that does work; the sizeof() operator is a compile time operator and it can
handle those things: it either takes a type or an expression as its arguments.
You probable overlooked the star in the '*m' expression.The sizeof operator
returns the size of the matrix in this case.

kind regards,

Jos
Jun 8 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
You probable overlooked the star in the '*m' expression.The sizeof operator
returns the size of the matrix in this case.
Yep, I did.

Good catch.
Jun 8 '07 #4

P: 5
Yes that does work; the sizeof() operator is a compile time operator and it can
handle those things: it either takes a type or an expression as its arguments.
You probable overlooked the star in the '*m' expression.The sizeof operator
returns the size of the matrix in this case.

kind regards,

Jos
I agree with you Jos it does work and I tested it.
What I did not get from the previous message is how to pass that pointer to the 2D array; to the fread function

here is part of my code:

#define LONMAX 1440
#define LATMAX60 480
typedef char matrix[LATMAX60][LONMAX];

void main (int argc, char *argv[])
{
.....
matrix1 *source60;
........
// Allocate memory for matrix variables
source60=malloc(sizeof *source60);

.........

nread = read3B4XRT1B (LATMAX60, fp, source);

.........

}

/* function read3B4XRT1B takes the name of the file to be read, the address of the 2D array to be written to and the size of the row to be read from the file and written to the 2D array*/
int read3B4XRT1B (
int latmax,
FILE *fp,
matrix *m)
{
int row, offset;
int nread;
for (row=0; row<latmax; row++)
{
nread = fread (*(m+row), 1, LONMAX, fp);
}
return nread;
}

problem: fread function is not working???

I hope that clear enough and I hope you can help me with that.
Thanks in advance.
mshaaban
Jun 8 '07 #5

Savage
Expert 100+
P: 1,764


Expand|Select|Wrap|Line Numbers
  1. int read3B4XRT1B (
  2.     int latmax,
  3.     FILE *fp,
  4.     matrix *m)
  5. {
  6.     int row, offset;
  7.     int nread;
  8.     for (row=0; row<latmax; row++)
  9.     {
  10.         nread = fread (*(m+row), 1, LONMAX, fp);
  11.     }
  12.     return nread;
  13. }
problem: fread function is not working???



I hope that clear enough and I hope you can help me with that.
Thanks in advance.
mshaaban
Read carefully post from weaknessforcats,part that's saying about 2D arrays.
Also if u wih taht ur function returns number of elements readed us should use then:

Expand|Select|Wrap|Line Numbers
  1.     int nread=0;
  2.     for (row=0; row<latmax; row++)
  3.     {
  4.         nread+ = fread (*(m+row), 1, LONMAX, fp);
  5.     }
  6.     return nread;
Savage
Jun 8 '07 #6

P: 5
Read carefully post from weaknessforcats,part that's saying about 2D arrays.
Also if u wih taht ur function returns number of elements readed us should use then:

Expand|Select|Wrap|Line Numbers
  1.     int nread=0;
  2.     for (row=0; row<latmax; row++)
  3.     {
  4.         nread+ = fread (*(m+row), 1, LONMAX, fp);
  5.     }
  6.     return nread;
Savage
I think I got it.
Thank you Savage and you too weahnessforcats.
Regards,
mshaaban
Jun 9 '07 #7

Savage
Expert 100+
P: 1,764
I think I got it.
Thank you Savage and you too weahnessforcats.
Regards,
mshaaban
We are more than happy to help u!!
:D

Savage
Jun 9 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.