473,322 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

pointer to typedef 2D array

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
7 11514
weaknessforcats
9,208 Expert Mod 8TB
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
JosAH
11,448 Expert 8TB
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
9,208 Expert Mod 8TB
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
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
1,764 Expert 1GB


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
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
1,764 Expert 1GB
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

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

Similar topics

2
by: Morten V Pedersen | last post by:
Hi all, I'm using the following code, but there's a problem somewhere. // Typedef af 8 arrays af 7 pointere til ints #include <iostream> using namespace std; int main()
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
5
by: Cancerbero | last post by:
Hi (first, excuse me for my bad english) As I know, the semantics for typedef is: typedef A B; I think this makes B a synonym of A, where A is an existing data type. Is that right? Based...
11
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
2
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
5
by: caleb.vandyke | last post by:
I am working with some code that is doing some pointer to structure casts and I can't figure out how the cast is being done. Here is basically the code. #include <stdio.h> #include <stdlib.h> ...
15
by: Paminu | last post by:
Still having a few problems with malloc and pointers. I have made a struct. Now I would like to make a pointer an array with 4 pointers to this struct. #include <stdlib.h> #include <stdio.h>...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
5
by: friend.05 | last post by:
1) #include <stdio.h> #include <stdlib.h> #include "graph.h" #define MAX_VTX 4 struct _graph_vertex {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.