Topic: Help: initializing array of void* pointers ...


Author: mmediko@hubcap.clemson.edu (Medikonda Muralidhar)
Date: 30 Mar 1994 05:42:44 GMT
Raw View
hello!
 I am trying to initialize an array of void *pointers just as we
typically would an array of character pointers and the compiler wouldnt
let me do it. For example

char *array[] = { "str1", "str2", 0,};  // fine!

int l = 10;  char a = 'a';
int lptr = &l; char *aptr = &a;

void *ptrs[] = {lptr, aptr, 0,};  // I cant do this!!

 However, I can declare an array of void * pointers (void * ptrs[10]),
and initialize each of the array elements individually, that compiles fine.
 I tried all ways of initializing, at the time of declaration, and
I am almost convinced it is not possible!

 My questions are :
1. Is my conclusion true?
2. If so, why can I initialize an array of character pointers and not an
array of void * pointers..

 Please enlighten me and thanks for your time!
Murali




Author: rudis+@cs.cmu.edu (Rujith S DeSilva)
Date: Wed, 30 Mar 1994 17:07:34 GMT
Raw View
In article <2nb3gk$6t1@hubcap.clemson.edu>,
Medikonda Muralidhar <mmediko@hubcap.clemson.edu> wrote:
>char *array[] = { "str1", "str2", 0,};  // fine!
>
>int l = 10;  char a = 'a';
>int lptr = &l; char *aptr = &a;
>void *ptrs[] = {lptr, aptr, 0,};  // I cant do this!!
>
>why can I initialize an array of character pointers and not an array of
>void * pointers..

The problem is not (void *), but the initialization.  For example:

char a = 'a';
char * aptr = &a;
char *ptrs[] = { aptr, 0 };     /* This doesn't work either. */

Error generated by gcc:
        initializer element for `ptrs[0]' is not constant

Later,
Rujith.
--

Rujith de Silva.  1-(412) 268-3620.  desilva+@cmu.edu
PGP public key available by finger.




Author: admin@rzaix13.uni-hamburg.de (Bernd Eggink)
Date: 31 Mar 1994 08:14:45 GMT
Raw View
Medikonda Muralidhar (mmediko@hubcap.clemson.edu) wrote:
>  hello!
>   I am trying to initialize an array of void *pointers just as we
>  typically would an array of character pointers and the compiler wouldnt
>  let me do it. For example

>  char *array[] = { "str1", "str2", 0,};  // fine!

>  int l = 10;  char a = 'a';
>  int lptr = &l; char *aptr = &a;

This is an error; you should write

int *lptr = &l;

>  void *ptrs[] = {lptr, aptr, 0,};  // I cant do this!!

This is completely legal and compiles without any problem. If your
compiler complaines, it must be broken.

>
>   However, I can declare an array of void * pointers (void * ptrs[10]),
>  and initialize each of the array elements individually, that compiles fine.
>   I tried all ways of initializing, at the time of declaration, and
>  I am almost convinced it is not possible!

>   My questions are :
>  1. Is my conclusion true?
>  2. If so, why can I initialize an array of character pointers and not an
>  array of void * pointers..

>   Please enlighten me and thanks for your time!
>  Murali

--
+----------------------------------+
|          Bernd Eggink            |
|    Rechenzentrum Uni Hamburg     |
| admin@rzaix13.rrz.uni-hamburg.de |
+----------------------------------+