473,395 Members | 1,568 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,395 software developers and data experts.

prob with int**

hello there ..
im not much an expert in C....im trying to use
int** for reperesentation of matrices(as double subscripted
arrays .)but at run time an error occurs ..saying ...general exception
error ...processor fault ....im using TURBO C v4.5 so plzif any body
could help me
Sep 21 '08 #1
15 1469
On Sep 21, 12:16*pm, the consiglieri <graciahell...@gmail.comwrote:
* * * * * * * * * * * im not much an expert in C.....im trying to use
int** for reperesentation of matrices(as double subscripted
arrays .)but at run time an error occurs ..saying ...general exception
error ...processor fault ....im using TURBO C v4.5 *so plzif any body
could help me
Post your code, we can't mind read.
Post in standard english. Nether "...." not "plzif" are standard
english.
--
Nick Keighley

Sep 21 '08 #2
the consiglieri wrote, On 21/09/08 12:16:
hello there ..
im not much an expert in C....im trying to use
int** for reperesentation of matrices(as double subscripted
arrays .)but at run time an error occurs ..saying ...general exception
error ...processor fault ....im using TURBO C v4.5 so plzif any body
could help me
There is an error on line 42. If I'm wrong try reading the comp.lang.c
FAQ at http://c-faq.com/ probably starting with section 6, but also 4
and 5. If you still can't fix it and the problem is not on line 42 post
a small compilable example that demonstrates the problem. After all,
without seeing your code all we can do is guess!
--
Flash Gordon
If spamming me sent it to sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Sep 21 '08 #3

"the consiglieri" <gr***********@gmail.comwrote in message
news:8a**********************************@d77g2000 hsb.googlegroups.com...
hello there ..
im not much an expert in C....im trying to use
int** for reperesentation of matrices(as double subscripted
arrays .)but at run time an error occurs ..saying ...general exception
error ...processor fault ....im using TURBO C v4.5 so plzif any body
could help me
int** requires that you manually allocate memory, which for a 2D array has
to be done in a structured form; have you done so?

--
Bartc

Sep 21 '08 #4

"the consiglieri" <gr***********@gmail.comwrote in message
hello there ..
im not much an expert in C....im trying to use
int** for reperesentation of matrices(as double subscripted
arrays .)but at run time an error occurs ..saying ...general exception
error ...processor fault ....im using TURBO C v4.5 so plzif any body
could help me
Hetre's how to do it.

remember to #include stdlib.h

int **mtx;

mtx = malloc(height * sizeof(int *));
if(!mtx)
/* out of memory error, handle here */
for(i=0;i<height;i++)
{
mtx[i] = malloc(width * sizeof(int));
if(!mtx[i])
/* out of memory, handle here */
}

/* ( initialise to zero) */
for(i=0;i,height;i++)
for(ii=0;ii<width;ii++)
mxt[i][ii] = 0;
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 21 '08 #5
On Sun, 21 Sep 2008 16:54:48 +0100, "Malcolm McLean"
<re*******@btinternet.comwrote:
>
"the consiglieri" <gr***********@gmail.comwrote in message
>hello there ..
im not much an expert in C....im trying to use
int** for reperesentation of matrices(as double subscripted
arrays .)but at run time an error occurs ..saying ...general exception
error ...processor fault ....im using TURBO C v4.5 so plzif any body
could help me

Hetre's how to do it.

remember to #include stdlib.h

int **mtx;

mtx = malloc(height * sizeof(int *));
if(!mtx)
/* out of memory error, handle here */
for(i=0;i<height;i++)
{
mtx[i] = malloc(width * sizeof(int));
if(!mtx[i])
/* out of memory, handle here */
}

/* ( initialise to zero) */
for(i=0;i,height;i++)
Obvious finger check. s/i,/i<
for(ii=0;ii<width;ii++)
mxt[i][ii] = 0;
--
Remove del for email
Sep 21 '08 #6
Bartc wrote:
"the consiglieri" <gr***********@gmail.comwrote in message
news:8a**********************************@d77g2000 hsb.googlegroups.com...
hello there ..
im not much an expert in C....im trying to use
int** for reperesentation of matrices(as double subscripted
arrays .)but at run time an error occurs ..saying ...general exception
error ...processor fault ....im using TURBO C v4.5 so plzif any body
could help me

int** requires that you manually allocate memory, which for a 2D array has
to be done in a structured form; have you done so?
I'm not sure what you mean by "manually allocate" and "structured".
Does the following code qualify?:

int row1[] = {1,2,3};
int row2[] = {-6,-5,-4};
int *rows[] = {row1, row2};
int **array = rows;
Sep 21 '08 #7

"Malcolm McLean" <re*******@btinternet.comwrote in message
news:N-*********************@bt.com...
>
"the consiglieri" <gr***********@gmail.comwrote in message
>hello there ..
im not much an expert in C....im trying to use
int** for reperesentation of matrices(as double subscripted
arrays .)but at run time an error occurs ..saying ...general exception
error ...processor fault ....im using TURBO C v4.5 so plzif any body
could help me
int **mtx;

mtx = malloc(height * sizeof(int *));
if(!mtx)
/* out of memory error, handle here */
for(i=0;i<height;i++)
{
mtx[i] = malloc(width * sizeof(int));
if(!mtx[i])
/* out of memory, handle here */
}

/* ( initialise to zero) */
for(i=0;i,height;i++)
for(ii=0;ii<width;ii++)
mxt[i][ii] = 0;
This is fiddly enough to be put into it's own function. Here's my code in
bad C. As written it requires the matrix to be indexed as [y][x] rather than
than [x][y] (but it's so long since I've had to subscript a matrix that I
can't remember the form).

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef int T; /* matrix element type */

T** allocmatrix(int rows, int columns) {
T **p;
int i;

if (rows<=0 || columns <=0) return NULL;

p=malloc(sizeof(T*)*rows);
if (p==NULL) return NULL;

for (i=0; i<rows; ++i) {
p[i]=malloc(sizeof(T)*columns);
if (p[i]==NULL) {
/* possibly free already allocated rows here, and p */
return NULL;
}
memset(p[i],0,sizeof(T)*columns);
}

return p;
}

void freematrix(T** p, int rows){
int i;

for (i=0; i<rows; ++i)
free(p[i]);

free(p);
}

int main(void){
#define rows 8
#define columns 12
T **mat;
int x,y;

mat=allocmatrix(rows,columns);

if (mat==NULL) exit(0);

for (y=0; y<rows; ++y)
for (x=0; x<columns; ++x)
mat[y][x]=y*1000+x;

for (y=0; y<rows; ++y) {
for (x=0; x<columns; ++x)
printf("%04d ",mat[y][x]);
puts("");
}

freematrix(mat,rows);

}

--
Bartc

Sep 21 '08 #8

<ja*********@verizon.netwrote in message
news:3b**********************************@p25g2000 hsf.googlegroups.com...
Bartc wrote:
>"the consiglieri" <gr***********@gmail.comwrote in message
news:8a**********************************@d77g200 0hsb.googlegroups.com...
hello there ..
im not much an expert in C....im trying to use
int** for reperesentation of matrices(as double subscripted
arrays .)but at run time an error occurs ..saying ...general exception
error ...processor fault ....im using TURBO C v4.5 so plzif any body
could help me

int** requires that you manually allocate memory, which for a 2D array
has
to be done in a structured form; have you done so?

I'm not sure what you mean by "manually allocate"
Use malloc explicitly.
>and "structured".
Make use of a row of pointers. See my other post for example.
Does the following code qualify?:

int row1[] = {1,2,3};
int row2[] = {-6,-5,-4};
int *rows[] = {row1, row2};
int **array = rows;
This is structured. But not manually allocated using malloc. I'm assuming
the matrix size is a variable (or is large) otherwise you can just declare a
regular matrix:

int matrix[6][9];

--
bartc

Sep 21 '08 #9

"Bartc" <bc@freeuk.comwrote in message
I'm assuming
the matrix size is a variable (or is large) otherwise you can just declare
a
regular matrix:

int matrix[6][9];
2D arrays have all sorts of problems. Firstly they cannot be resized.
Secondly the syntax for passing them to other functions is so complex as to
be effectively unusable. Thirdly the subroutines have to be written to take
fixed-width matrices.
Arrays of pointer, whilst not ideal, get around many of these problems.

My favoured solution is to just allocate a flat buffer and calculate the
offset

matrix[y*height+x] = val;
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 21 '08 #10
Malcolm McLean wrote, On 21/09/08 16:54:
>
"the consiglieri" <gr***********@gmail.comwrote in message
>hello there ..
im not much an expert in C....im trying to use
int** for reperesentation of matrices(as double subscripted
arrays .)but at run time an error occurs ..saying ...general exception
error ...processor fault ....im using TURBO C v4.5 so plzif any body
could help me

Hetre's how to do it.
Well, it is *one* way to do it, but not the only way. The comp.lang.c
FAQ has more methods at hrrp://c-faq.com/
remember to #include stdlib.h

int **mtx;

mtx = malloc(height * sizeof(int *));
Better would be
mtx = malloc(height * sizeof *mtx);
if(!mtx)
/* out of memory error, handle here */
Note that the out of memory handler needs to stop it from executing the
code below.
for(i=0;i<height;i++)
{
mtx[i] = malloc(width * sizeof(int));
<snip>

Here it would be better to have
mtx[i] = malloc(width * sizeof *mtx[1]);

Note the how many times Malcolm had to get the type right compared to
how many times I need to.

Also depending on what you are trying to do it could be the wrong way.
--
Flash Gordon
If spamming me sent it to sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Sep 21 '08 #11

"Malcolm McLean" <re*******@btinternet.comwrote in message
news:a7******************************@bt.com...
>
"Bartc" <bc@freeuk.comwrote in message
>I'm assuming
the matrix size is a variable (or is large) otherwise you can just
declare a
regular matrix:

int matrix[6][9];
2D arrays have all sorts of problems. Firstly they cannot be resized.
Secondly the syntax for passing them to other functions is so complex as
to be effectively unusable. Thirdly the subroutines have to be written to
take fixed-width matrices.
Arrays of pointer, whilst not ideal, get around many of these problems.

My favoured solution is to just allocate a flat buffer and calculate the
offset

matrix[y*height+x] = val;
Don't you mean, y*width?

And to pass this to a function still requires this extra parameter, and
possibly height too, depending on what the function does.

(You also have the nuisance of having this extra value: height or width,
associated with each matrix access, and the overhead of an extra multiply.)

--
Bartc
Sep 21 '08 #12

"Bartc" <bc@freeuk.comwrote in message news
>
"Malcolm McLean" <re*******@btinternet.comwrote in message
news:a7******************************@bt.com...
>>
"Bartc" <bc@freeuk.comwrote in message
>>I'm assuming
the matrix size is a variable (or is large) otherwise you can just
declare a
regular matrix:

int matrix[6][9];
2D arrays have all sorts of problems. Firstly they cannot be resized.
Secondly the syntax for passing them to other functions is so complex as
to be effectively unusable. Thirdly the subroutines have to be written to
take fixed-width matrices.
Arrays of pointer, whilst not ideal, get around many of these problems.

My favoured solution is to just allocate a flat buffer and calculate the
offset

matrix[y*height+x] = val;

Don't you mean, y*width?

And to pass this to a function still requires this extra parameter, and
possibly height too, depending on what the function does.

(You also have the nuisance of having this extra value: height or width,
associated with each matrix access, and the overhead of an extra
multiply.)
Yes I do, which is the disadvantage of the manaul method. It's too easy to
make that type of slip.
The multiplication overhead is a type of micro-optimisation. Whilst there
are circumstances where it can make or break a routine, generally that's
what you worry about after you have a nice functioning program. Sometimes
there is a hidden multiply in a 2D array access, though I admit it is easier
for the compiler to optiise out the constant.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 21 '08 #13
"Malcolm McLean" <re*******@btinternet.comwrites:
"Bartc" <bc@freeuk.comwrote in message
>I'm assuming
the matrix size is a variable (or is large) otherwise you can just
declare a
regular matrix:

int matrix[6][9];
2D arrays have all sorts of problems. Firstly they cannot be
resized.
I think this is a red-herring. Neither can 1D arrays. It is always
going to be a bit fiddly to resize 2D arrays in C no matter what
method you use to implement them and your suggestion does not make it
any easier than the "natural" way (see below).
Secondly the syntax for passing them to other functions is so
complex as to be effectively unusable.
No so in C99, and a temporary typedef can be used to make even the
allocation be neat and simple:

int main(int argc, char **argv)
{
size_t n = argc 1 ? atoi(argv[1]) : 4;
size_t m = argc 2 ? atoi(argv[2]) : 5;
typedef double Array[n][m];
Array *ap = malloc(sizeof *ap);
fill(n, m, *ap);
print(n, m, *ap);
return 0;
}

How hard is that? It looks logical and consistent. The function
prototypes are not obscure either[1]:

void fill(size_t n, size_t m, double array[n][m]);
void print(size_t n, size_t m, double array[n][m]);
Thirdly the subroutines have to
be written to take fixed-width matrices.
Again, not in C99. Now I know that C99 is not as portable as C90 but I
think it deserves a fair hearing. This is one thing it does
reasonable well. The fill function might just be:

void fill(size_t n, size_t m, double array[n][m])
{
for (size_t r = 0; r < n; r++)
for (size_t c = 0; c < n; c++)
array[r][c] = (r + 1) * (c + 1);
}

When doing numerical work with arrays in C, C99's features deserve
serious consideration.

To the OP: do consider using C99 if it is available to you.

[1] The form using * is rather less obvious until you think of it is
simply a way to say "some parameter -- the name does not matter":

void fill(size_t, size_t, double array[*][*]);

--
Ben.
Sep 21 '08 #14
On Sun, 21 Sep 2008 18:50:44 +0100, "Malcolm McLean"
<re*******@btinternet.comwrote:
>
"Bartc" <bc@freeuk.comwrote in message
>I'm assuming
the matrix size is a variable (or is large) otherwise you can just declare
a
regular matrix:

int matrix[6][9];
2D arrays have all sorts of problems. Firstly they cannot be resized.
Isn't the same true for 1D arrays?
>Secondly the syntax for passing them to other functions is so complex as to
You really consider
int func(int x[][9]){...}
to be too complex to use?
>be effectively unusable. Thirdly the subroutines have to be written to take
fixed-width matrices.
Arrays of pointer, whilst not ideal, get around many of these problems.

My favoured solution is to just allocate a flat buffer and calculate the
offset

matrix[y*height+x] = val;
--
Remove del for email
Sep 21 '08 #15
Ben Bacarisse <be********@bsb.me.ukwrites:
"Malcolm McLean" <re*******@btinternet.comwrites:
"Bartc" <bc@freeuk.comwrote in message
I'm assuming
the matrix size is a variable (or is large) otherwise you can just
declare a
regular matrix:

int matrix[6][9];
2D arrays have all sorts of problems. Firstly they cannot be
resized.

I think this is a red-herring. Neither can 1D arrays. [...]
A 1D array (at least one allocated with malloc) can be
resized easily enough by using realloc.
Oct 9 '08 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Junior | last post by:
I keep receiving this "The type or namespace name 'CASsEventHandler' could not be found (are you missing a using directive or an assembly reference?)" message in two particular lines, and I've...
5
by: David Sobey | last post by:
Hi Sorry bout this basic prob. Got a file called file.obj. tryna read the first line from it as a string and print it to the screen. getting errors: #include "stdafx.h" #include <stdio.h>...
7
by: Dave | last post by:
hi another simple problem sorry. i've got a string, "Buffer", and an int, Loop. Somehow, this: Buffer.length<=Loop+1 gives this: error C2296: '<=' : illegal, left operand has type...
2
by: Dishan Fernando | last post by:
Hi my prob is like this.. ----------------------------- create table ax( i int , j int ) create table ay( i int ,
2
by: Gernot Frisch | last post by:
I'm using #~*&$§ VC6 and have this problem: // good std::deque<A_POINT>::const_iterator it = p.POINTS.begin(); // bad std::deque<A_POINT>::const_reverse_iterator it = p.POINTS.rbegin(); ...
14
by: Matt Suther | last post by:
I just bought The C Programming Language, by the guy who made C and so...I have 2 different compilers, free ones, and I don't know if I typed the code in wrong or if it's a problem with my...
28
by: Vishal Naidu | last post by:
i m new to the C world... i ve been told by my instructors not to use goto stmt.. but no one could give me a satisfactory answer as to why it is so.. plz help me out of this dilemma, coz i use...
2
by: jhetfield18 | last post by:
I have to write a program for the university as a weekly project that gets an int array as an input and the contents can only be 0 and 1.Its supposed to be a maze solver.1 as wall and you can move on...
8
by: sachinv1821 | last post by:
hi all i have simple Problem please tell me the Solution if u know?? main() { int a={1,2,3,4,5,6,7,8,9}; printf("%u %u %u",a,a,a); } when i execute this program i am getting a Fixed address...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.