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
7 11514
You have several issues here:
This code does not work: -
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: -
char matrix[matrix_size][matrix_size];
-
This array has matrix_size times matrix_size number of elements. So your malloc shoulde be: -
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.: -
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: -
char arr[6] = {'a','b','c','d','e','f'};
-
has a memory layout of
a b c d e f
whereas this array: -
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. -
char arr[3][2];
-
fread( (void*)&arr[0][0], 1, 6), pfile);
-
You have several issues here:
This code does not work: -
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
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.
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
- 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
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: - int nread=0;
-
for (row=0; row<latmax; row++)
-
{
-
nread+ = fread (*(m+row), 1, LONMAX, fp);
-
}
-
return nread;
Savage
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: - int nread=0;
-
for (row=0; row<latmax; row++)
-
{
-
nread+ = fread (*(m+row), 1, LONMAX, fp);
-
}
-
return nread;
Savage
I think I got it.
Thank you Savage and you too weahnessforcats.
Regards,
mshaaban
I think I got it.
Thank you Savage and you too weahnessforcats.
Regards,
mshaaban
We are more than happy to help u!!
:D
Savage
Sign in to post your reply or Sign up for a free account.
Similar topics
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()
|
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.
...
|
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...
|
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);
}
|
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.
...
|
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>
...
|
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>...
|
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:
...
|
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...
|
by: friend.05 |
last post by:
1)
#include <stdio.h>
#include <stdlib.h>
#include "graph.h"
#define MAX_VTX 4
struct _graph_vertex {
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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...
|
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...
| |