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
+ 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
7 Replies

 Expert Mod 5K+ P: 9,197 You have several issues here: This code does not work: Expand|Select|Wrap|Line Numbers matrix *m; m =malloc(sizeof *m); //Actually i'm using a code from one of your posting   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 char matrix[matrix_size][matrix_size];   This array has matrix_size times matrix_size number of elements. So your malloc shoulde be: Expand|Select|Wrap|Line Numbers matrix* m = (matrix*)malloc(sizeof(char) * (matrix_size * matrix_size));   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 char arr[6]; fread( (void*)arr, 1, 6), pfile);      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 char arr[6] = {'a','b','c','d','e','f'};   has a memory layout of a b c d e f whereas this array: Expand|Select|Wrap|Line Numbers char arr[3][2] = {'a','b','c','d','e','f'};   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 char arr[3][2]; fread( (void*)&arr[0][0], 1, 6), pfile);      Jun 8 '07 #2

 Expert 10K+ P: 11,448 You have several issues here: This code does not work: Expand|Select|Wrap|Line Numbers matrix *m; m =malloc(sizeof *m); //Actually i'm using a code from one of your posting   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

 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

 Expert 100+ P: 1,764 Expand|Select|Wrap|Line Numbers int read3B4XRT1B (     int latmax,     FILE *fp,     matrix *m) {     int row, offset;     int nread;     for (row=0; row

 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     int nread=0;     for (row=0; row

 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.