Help : Pointer to 2D Array 
Author Message
 Help : Pointer to 2D Array

Hie,

Could anyone tell me what's wrong with this code ? I get a
segmentation fault at the beginnign of the first set of "for loops". I
am trying to run this code on HP-Unix running GNU.

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

 void main()
 {
   int *(myarr)[5];
   int i,j,rows;

   rows=3;

 /* Allocate Memory for the Matrix */

  *myarr=(int *)malloc( rows * rows * sizeof(int) );

cout << "\n Memory allocated ";
/* Set all the array elements to 0 */

 for(i=0;i<rows;i++)
  for(j=0;j<rows;j++)
   *( *(myarr+i)+j)=0;

 for(i=0;i<rows;i++) {
  for(j=0;j<rows;j++)
   printf("%d", *( *(myarr+i) + j));
  cout << endl;
 }

Quote:
}



Sat, 24 Sep 2005 23:35:25 GMT  
 Help : Pointer to 2D Array


Quote:
> Hie,

> Could anyone tell me what's wrong with this code ? I get a
> segmentation fault at the beginnign of the first set of "for loops". I
> am trying to run this code on HP-Unix running GNU.

> #include<iostream.h>

What?

Quote:
> #include<stdio.h>
> #include<stdlib.h>

>  void main()

main returns int

Quote:
>  {
>    int *(myarr)[5];
>    int i,j,rows;

>    rows=3;

>  /* Allocate Memory for the Matrix */

>   *myarr=(int *)malloc( rows * rows * sizeof(int) );

don't cast the return from malloc.

Quote:
> cout << "\n Memory allocated ";

What?

Quote:
> /* Set all the array elements to 0 */

>  for(i=0;i<rows;i++)
>   for(j=0;j<rows;j++)
>    *( *(myarr+i)+j)=0;

Boom!

Quote:

>  for(i=0;i<rows;i++) {
>   for(j=0;j<rows;j++)
>    printf("%d", *( *(myarr+i) + j));
>   cout << endl;

What?

Quote:
>  }

Return???
Quote:
> }



Sun, 25 Sep 2005 00:18:56 GMT  
 Help : Pointer to 2D Array

Quote:
> Hie,
> Could anyone tell me what's wrong with this code ? I get a
> segmentation fault at the beginnign of the first set of "for loops". I
> am trying to run this code on HP-Unix running GNU.
> #include<iostream.h>

Not a standard C header.

Quote:
> #include<stdio.h>
> #include<stdlib.h>
>  void main()

Undefined behaviour. Use int main().

Quote:
>  {
>    int *(myarr)[5];
>    int i,j,rows;
>    rows=3;
>  /* Allocate Memory for the Matrix */
>   *myarr=(int *)malloc( rows * rows * sizeof(int) );

Because you have #included <stdlib.h>, you can safely drop the cast to
(int *).
NOTE that myarr is an unitialised pointer. Attempting to write to
*myarr causes undefined behaviour, which is most likely crashing your
program.
First allocate space for myarr itself, then for *myarr.

Quote:
> cout << "\n Memory allocated ";

You haven't declared cout anywhere. Anyway, left-shifting with a
string literal is a constraint violation.

Quote:
> /* Set all the array elements to 0 */
>  for(i=0;i<rows;i++)
>   for(j=0;j<rows;j++)
>    *( *(myarr+i)+j)=0;

Because you have already caused undefined behaviour by using *myarr
without initialising myarr, this loop is entirely pointless.

Quote:
>  for(i=0;i<rows;i++) {
>   for(j=0;j<rows;j++)
>    printf("%d", *( *(myarr+i) + j));

And so is this one.

Quote:
>   cout << endl;

cout and endl are undefined symbols.

Quote:
>  }
> }

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"B-but Angus! You're a dragon!"
   - Mickey Mouse


Sun, 25 Sep 2005 00:22:29 GMT  
 Help : Pointer to 2D Array
Instead of  :
 for(i=0;i<rows;i++)
      for(j=0;j<rows;j++)
       *( *(myarr+i)+j)=0;

try
 for(i=0;i<rows;i++)
      for(j=0;j<rows;j++)
       *( (myarr+j)+i*rows)=0;

The program crashes because you are dereferencing an int, *(myarr+i)+j, to
an int pointer!!!
You can also clear the matrix by simply doing:
    memset(myarr,0,row*row*sizeof(int));

-AG


Quote:
> Hie,

> Could anyone tell me what's wrong with this code ? I get a
> segmentation fault at the beginnign of the first set of "for loops". I
> am trying to run this code on HP-Unix running GNU.

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

>  void main()
>  {
>    int *(myarr)[5];
>    int i,j,rows;

>    rows=3;

>  /* Allocate Memory for the Matrix */

>   *myarr=(int *)malloc( rows * rows * sizeof(int) );

> cout << "\n Memory allocated ";
> /* Set all the array elements to 0 */

>  for(i=0;i<rows;i++)
>   for(j=0;j<rows;j++)
>    *( *(myarr+i)+j)=0;

>  for(i=0;i<rows;i++) {
>   for(j=0;j<rows;j++)
>    printf("%d", *( *(myarr+i) + j));
>   cout << endl;
>  }

> }



Sun, 25 Sep 2005 00:36:34 GMT  
 Help : Pointer to 2D Array

Quote:
> Instead of  :
>  for(i=0;i<rows;i++)
>       for(j=0;j<rows;j++)
>        *( *(myarr+i)+j)=0;
> try
>  for(i=0;i<rows;i++)
>       for(j=0;j<rows;j++)
>        *( (myarr+j)+i*rows)=0;
> The program crashes because you are dereferencing an int, *(myarr+i)+j, to
> an int pointer!!!

The program crashes way before that. See my reply for an explanation.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"Immanuel Kant but Genghis Khan."
   - The Official Graffitist's Handbook



Sun, 25 Sep 2005 00:46:50 GMT  
 Help : Pointer to 2D Array

Quote:

>> Instead of  :
>>  for(i=0;i<rows;i++)
>>       for(j=0;j<rows;j++)
>>        *( *(myarr+i)+j)=0;
>> try
>>  for(i=0;i<rows;i++)
>>       for(j=0;j<rows;j++)
>>        *( (myarr+j)+i*rows)=0;
>> The program crashes because you are dereferencing an int, *(myarr+i)+j, to
>> an int pointer!!!
> The program crashes way before that. See my reply for an explanation.

Or does it? Or DOES it? OR DOES IT?
What is int *(myarr)[5]? int *myarr[5] is an array of 5 pointers to int,
int (*myarr)[5] is a pointer to an array of 5 ints, but what is the
declaration in the OP's code?
I might have spewed forth complete nonsense!

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"Nothing lasts forever - so why not destroy it now?"
   - Quake



Sun, 25 Sep 2005 00:48:15 GMT  
 Help : Pointer to 2D Array
*myarr=(int *)malloc( rows * rows * sizeof(int) );
is incorrect as well.
It should be:
myarr=malloc(rows*rows*sizeof(int));

As Joona mentioned, the declaration is also wrong.

-AG


Quote:
> Instead of  :
>  for(i=0;i<rows;i++)
>       for(j=0;j<rows;j++)
>        *( *(myarr+i)+j)=0;

> try
>  for(i=0;i<rows;i++)
>       for(j=0;j<rows;j++)
>        *( (myarr+j)+i*rows)=0;

> The program crashes because you are dereferencing an int, *(myarr+i)+j, to
> an int pointer!!!
> You can also clear the matrix by simply doing:
>     memset(myarr,0,row*row*sizeof(int));

> -AG



> > Hie,

> > Could anyone tell me what's wrong with this code ? I get a
> > segmentation fault at the beginnign of the first set of "for loops". I
> > am trying to run this code on HP-Unix running GNU.

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

> >  void main()
> >  {
> >    int *(myarr)[5];
> >    int i,j,rows;

> >    rows=3;

> >  /* Allocate Memory for the Matrix */

> >   *myarr=(int *)malloc( rows * rows * sizeof(int) );

> > cout << "\n Memory allocated ";
> > /* Set all the array elements to 0 */

> >  for(i=0;i<rows;i++)
> >   for(j=0;j<rows;j++)
> >    *( *(myarr+i)+j)=0;

> >  for(i=0;i<rows;i++) {
> >   for(j=0;j<rows;j++)
> >    printf("%d", *( *(myarr+i) + j));
> >   cout << endl;
> >  }

> > }



Sun, 25 Sep 2005 01:12:42 GMT  
 Help : Pointer to 2D Array


Quote:
>Or does it? Or DOES it? OR DOES IT?
>What is int *(myarr)[5]? int *myarr[5] is an array of 5 pointers to int,
>int (*myarr)[5] is a pointer to an array of 5 ints, but what is the
>declaration in the OP's code?
>I might have spewed forth complete nonsense!

I believe int *(myarr)[5] and int *myarr[5] are equivalent, that is,
the parentheses are unnecessary and have no effect.

It looks like all the OP wanted was a two-dimensional array
of integers (based on the size of the malloc() call) so the
declaration, of course, is wrong and should be just an int*.

If the assignments of array values had been any other integer
than 0, then the compiler would have no doubt complained.

I'm guessing the code was taken from some other program,
modified, and then tinkered with by adding and removing asterisks and
parentheses until the compiler quit complaining.  Otherwise it's hard
to explain how the OP came up with such an elaborate declaration.



Sun, 25 Sep 2005 01:50:17 GMT  
 Help : Pointer to 2D Array

Quote:

> Hie,

> Could anyone tell me what's wrong with this code ?

First of all, it seems to be written in c/c++.
Despite the fact that there is no such language,
some c/c++ code manages to get posted anyway.

Quote:
> I get a
> segmentation fault at the beginnign of the first set of "for loops". I
> am trying to run this code on HP-Unix running GNU.

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

>  void main()
>  {
>    int *(myarr)[5];
>    int i,j,rows;

>    rows=3;

>  /* Allocate Memory for the Matrix */

>   *myarr=(int *)malloc( rows * rows * sizeof(int) );

> cout << "\n Memory allocated ";
> /* Set all the array elements to 0 */

>  for(i=0;i<rows;i++)
>   for(j=0;j<rows;j++)
>    *( *(myarr+i)+j)=0;

>  for(i=0;i<rows;i++) {
>   for(j=0;j<rows;j++)
>    printf("%d", *( *(myarr+i) + j));
>   cout << endl;
>  }

> }

/* BEGIN new.c */

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

#define COLUMNS 5

int main(void)
 {
     int (*myarr)[COLUMNS];
     int i, j, rows;

     rows=3;

     /* Allocate Memory for the Matrix */

     myarr = malloc(rows * sizeof *myarr);    
     puts("\n Memory allocated ");

     for(i=0;i<rows;i++)
     for(j=0;j<COLUMNS;j++)
         myarr[i][j] = i + j;

     for(i=0;i<rows;i++) {
         for(j=0;j<COLUMNS;j++) {
             printf("%d ", myarr[i][j]);
         }
         putchar('\n');
     }
     return 0;

Quote:
}

/* END new.c */

--
pete



Sun, 25 Sep 2005 07:18:21 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. 2D array of pointers to 2D arrays

2. Help needed: 2D arrays and pointers in ANSI C.

3. HELP: Pointer to array of 2D.

4. Which 2D array of 2D array addressing method?

5. char pointer to 2D char array

6. Pointers to 2D arrays

7. converting 2D array to pointer

8. a dynamical 2D array to a double pointer

9. Pointer to 2D array...

10. HOWTO:2d array of pointers to structure

11. pointer arithmetic with 2d arrays?

12. Pointer of a 2d array

 

 
Powered by phpBB® Forum Software