home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!haven.umd.edu!darwin.sura.net!zaphod.mps.ohio-state.edu!cs.utexas.edu!sun-barr!ames!nsisrv!ame.gsfc.nasa.gov!almeida
- From: almeida@ame.gsfc.nasa.gov (aswin m. almeida)
- Subject: The 2-Dee Array Deelemna
- Message-ID: <1993Jan6.190355.9632@nsisrv.gsfc.nasa.gov>
- Sender: usenet@nsisrv.gsfc.nasa.gov (Usenet)
- Nntp-Posting-Host: ame.gsfc.nasa.gov
- Organization: NASA Goddard
- Date: Wed, 6 Jan 1993 19:03:55 GMT
- Lines: 102
-
- I would like to thank those of you who helped me earlier
- with my buggy malloctest program that was an attempt at
- declaring a two dimensional array on the fly. Using
- your suggestions and corrections, I came up with the
- following fragment of code, which runs, and then gets
- a bus error (I am curious as to what is causing this
- bus error).
-
-
- PROGRAM: (Corrected as by your mail to me)
-
-
- In addition, I would greatly appreciate it if someone
- could explain (with crude ASCII drawings) *, **, and &.
- I find it easiest to learn with drawings when it comes
- to pointers (its how I learned PASCAL), but C is a
- new cup of tea, and a picture is worth a thousand words <:
-
-
- more malloctest.c
- #include <stdio.h>
-
- main ()
- {
-
- int i,j;
-
- int nrows, ncolumns;
- int **array; /* 1) corrected, moved from 2) below */
-
- ncolumns = 10;
-
- printf ("Enter array index: \n");
- scanf ("%d", &nrows);
-
-
- array = (int **) malloc (nrows * sizeof(int *)); /* 2) int declaration moved to 1) */
- for (i=0; i < nrows; i++)
- array[i] = (int *) malloc (ncolumns * sizeof(int)); /* removed * before last int */
-
- printf ("Reading array values, and printing\n");
- for (i=0; i<10; i++)
- for (j=0; j<nrows; j++)
- {
- printf("%d x %d = %d\n",i,j,i*j);
- array[i][j] = i*j; /* dbx reports this as the location of the bus error */
- printf("%d,%d = %d\n",i,j,array[i][j]);
-
- }
-
- }
-
- Ok, basically, this is the program. I moved int **array up with the other declarations,
- as you all told me to do, and removed a * before the last int (I would greatly
- appreciate a good explanation of why this is so).
-
- Here is the input/output:
-
- a.out
- Enter array index:
- 3
- Reading array values, and printing
- 0 x 0 = 0
- 0,0 = 0
- 0 x 1 = 0
- 0,1 = 0
- 0 x 2 = 0
- 0,2 = 0
- 1 x 0 = 0
- 1,0 = 0
- 1 x 1 = 1
- 1,1 = 1
- 1 x 2 = 2
- 1,2 = 2
- 2 x 0 = 0
- 2,0 = 0
- 2 x 1 = 2
- 2,1 = 2
- 2 x 2 = 4
- 2,2 = 4
- 3 x 0 = 0
- Bus error (core dumped)
-
- What gives here?
-
- Again, I would like to thank those of you who responded and helped with
- debugging this small fragment of code. Now I am looking for
-
- 1) The reason I am getting this annoying bus error.
- 2) An explanation, with crude ASCII pictures, of *, **, and & in C.
- 3) An explanation of why | array[i] = (int *) malloc (ncolumns * sizeof(int));
- I was told to remove the star here ----------------------------------^ before int.
-
- I have a few better C books now (went to Crown and picked Crash Course in C
- and a Microsoft C book by McGrawHill), and I also have the C bible, though
- I am not a computer jock (yet <: ) and its not really for me. I'd
- appreciate any comments or suggestions directly to
-
- almeida@ame.gsfc.nasa.gov
-
- Thanks!
- A. Almeida
-