Newbie Array Problem BC/C++ v4.0 
Author Message
 Newbie Array Problem BC/C++ v4.0

Here goes:

main()
{
   void foo(char *);
   char MyArray[2][50];
   char *ptrMyArray;

   ptrMyArray = MyArray;         generates "suspicious pointer conversion"

                        foo(ptrMyArray);

Quote:
}

void foo(char *)
{
. . .

Quote:
}

I give up. Also, I had to modify the orginal code so that foo
accepted a pointer variable instead of a pointer constant. I would
prefer to pass foo MyArray (a pointer constant) instead of
ptrMyArray (a pointer variable).

--
email:



fax:
(404)494-3182



Sun, 09 Feb 1997 22:59:19 GMT  
 Newbie Array Problem BC/C++ v4.0
Hello


Quote:
>   char MyArray[2][50];

This is a two dimensional array.  If you think about it in terms of pointers, what
does *(Myarray+1) equal??

Quote:
>   char *ptrMyArray;

This is a one dimensional array.  What does *(ptrMyArray+1) equal??

The first is a char *.

The second in a char

Quote:
>   ptrMyArray = MyArray;         generates "suspicious pointer conversion"

It should this is not doing what you want!
You want ptrMyArray to be a char **.

Soren Dayton



Mon, 10 Feb 1997 04:58:29 GMT  
 Newbie Array Problem BC/C++ v4.0
 Hi,

 I am trying to allocate memory for a pointer to an array of NCOL chars.
 According to the FAQ the following should work:

   #define NCOL 10

   int main ()
   {
      int  NROWS = 10;
      char (*pa)[NCOL];   /* pa is pointer to char array of size NCOL */

      pa = (char (*)[NCOL]) malloc (NROWS * sizeof (*pa));

      /* other statements */
   }

 I get no compilation errors or warnings for this code fragment using either
 cc or gcc under Ultrix on a Dec5000, but when I try to inspect the size of
 (*pa), I get an answer of 40. On my machine, sizeof(char) == 1. Shouldn't
 I get an answer of 10, or am I missing something?

 Something else: once I have allocated memory for pa, what is the proper
 way of reading in values? Would  strcpy(pa[i],string) work?  

 Please enlighten me on the above subjects. Thanks in advance,

 Nikolas.



Mon, 10 Feb 1997 07:51:07 GMT  
 Newbie Array Problem BC/C++ v4.0

Quote:

>main()
>{
>   void foo(char *);
>   char MyArray[2][50];
>   char *ptrMyArray;

>   ptrMyArray = MyArray;         generates "suspicious pointer conversion"

>                    foo(ptrMyArray);
>}

>void foo(char *)
>{
>.. . .
>}

It depends exactly what you want to do but the pointer conversion is
suspicious, no doubt about it.

        char Array[20];         /* Defines an array     */
        char (*ArrayPtr)[];     /* Defines a pointer    */

        Array[2] = 'a';
        ArrayPtr = &Array;

        printf("%c\n", ((*ArrayPtr)[2]));

With two dimensional arrays the compiler must know the size of the dimensions
in order to dereference the arrays, in the above example there is no need
to specify it (i.e. I used [] in the ArrayPtr definition) even so it works
in the same way.

Keith



Mon, 10 Feb 1997 14:20:49 GMT  
 Newbie Array Problem BC/C++ v4.0

Quote:

>   char MyArray[2][50];
>   char *ptrMyArray;
>   ptrMyArray = MyArray;         generates "suspicious pointer conversion"

That's because MyArray can be assigned to a pointer to arrays
of 50 chars with no problem, but not to a pointer to char.

So, if you declare ptrMyArray as:

    char (* ptrMyArray)[ 50 ];

the problem will go away. foo() could be declared as

    void foo( char (*)[ 50 ] );

    foo(MyArray);

Quote:
>I give up. Also, I had to modify the orginal code so that foo
>accepted a pointer variable instead of a pointer constant. I would
>prefer to pass foo MyArray (a pointer constant) instead of
>ptrMyArray (a pointer variable).

Try

#include <stdio.h>

void foo( char (* const p)[50] )
{
        printf( "%c\n", p[1][49] );

Quote:
}

int main( void )
{
        char aach[ 2 ][ 50 ];

        aach[1][49] = 'O';
        foo( aach );
        return 0;

Quote:
}

--
| Kurt Watzka                             Phone : +49-89-2180-2158




Mon, 10 Feb 1997 19:33:01 GMT  
 Newbie Array Problem BC/C++ v4.0

   Here goes:

   main()
   {
      void foo(char *);
      char MyArray[2][50];
      char *ptrMyArray;

      ptrMyArray = MyArray;         generates "suspicious pointer conversion"

                           foo(ptrMyArray);
   }

   void foo(char *)
   {
   . . .
   }

The problem is that MyArray is not really a char *.  It is
a char (*)[50], that is, a pointer to an array of 50
characters.  You should either change foo() to accept char (*)[50],
or change MyArray to char [100].

Ed Karrels



Tue, 11 Feb 1997 13:55:32 GMT  
 Newbie Array Problem BC/C++ v4.0

Quote:
>Here goes:
>main()
>{
>   void foo(char *);
>   char MyArray[2][50];
>   char *ptrMyArray;
>   ptrMyArray = MyArray;         generates "suspicious pointer conversion"

It is.  'MyArray' is char[][], which can decay into 'char **' in expressions.
'ptrMyArray' is char[], which can decay into 'char *' in expressions.

Quote:
>I give up. Also, I had to modify the orginal code so that foo
>accepted a pointer variable instead of a pointer constant.
>I would prefer to pass foo MyArray (a pointer constant) instead of
>ptrMyArray (a pointer variable).

'MyArray' as declared is an array of objects of type array of char, i.e.,
it's a (nearly, though technically not) multi-dimensional array.  This
is not legitimately equivalent to a 'char *'.  Try 'ptrMyArray = MyArray[0];'
and see if that helps.

Quote:
>email:


-seebs
--

GAT(CS/M/P/SS) d-- H++ s+: !g p? !au a- w+++ v+++/* C++++ UB/V++++ P+ L b+++ !D
3+(NetBSD/Amiga) E- N+++ K !W--- M++/-- V- -po+ Y+ t 5++ jx R G'''' tv- B---
e++ u** h--- f+ r+++ !n x+++*   --SeebS-- / The Laughing Prophet


Sun, 16 Feb 1997 07:21:04 GMT  
 Newbie Array Problem BC/C++ v4.0

Quote:


> >Here goes:
> >main()
> >{
> >   void foo(char *);
> >   char MyArray[2][50];
> >   char *ptrMyArray;
> >   ptrMyArray = MyArray;         generates "suspicious pointer conversion"
> It is.  'MyArray' is char[][], which can decay into 'char **' in expressions.
> 'ptrMyArray' is char[], which can decay into 'char *' in expressions.

MyArray can not decay into char **.  If you have a C compiler which converts
char [][50] into a char **, get rid if it.  In most expressions, MyArray
will be automatically converted into a char (*)[50].

--
Mike Rubenstein



Tue, 18 Feb 1997 21:36:38 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Borland C++ V4.5 Max Array size

2. question regarding BC v4.0

3. f2c convertor with bc++v4.

4. problem running MFC v4.0 ODBC app on MFC v4.2 machine

5. Linker problem with Borland C++ v4.00

6. Array Problem in BC++3.0

7. Borland c++ v4.02 DOS Powerpack Key map problem

8. Newbie Array Problem - how to strncpy from an array of char

9. Visual C++ V4.0 vs Borland C++ V5.0

10. Newbie VC++ V4 BUILD error

11. Newbie Problem with Borland Turbo C++ and MS Visual C++

12. Help a Newbie at Visual C++ (I am not a newbie at C++)

 

 
Powered by phpBB® Forum Software