problems when initializing pointers to byte arrays 
Author Message
 problems when initializing pointers to byte arrays

I encountered a problem in C when initializing byte-arrays.

     char s1[] = {"abc"};

This statement works fine.
If I want to set each byte separately i.e. don't want to have
a "" terminated string I can write:

     char s2[] = {'a', 'b', 'c'};

If I want to initialize a pointer to point to a string array I write:

     char *s3  = {"abc"};

Now if I want to do the same with 2nd example above then I run into problems:

     char *s4  = {'a', 'b', 'c'};

Apparently this line is illegal in C ?
All the compilers I tested are generating an error message.
Also the ANSI C-spec does not help me here either.

Is there any way to initializing a pointer to an byte array.
In this example I used ASCII-characters, but I want to put in
hex values in my real application which looks like this:

     unsigned char *b[] = {12, 13, 14, 15, 16, 16, 17, 17, 18, 19, 20},

Actually I want to set up a pointer array where each element points to
a byte array.

Has anybody out there any clue if/how it can be done?

Jurgen

-------------------------------------------------------------------------------
Jurgen Bartels,   Motorola,  Taunusstein near Wiesbaden  Germany



-------------------------------------------------------------------------------



Sun, 27 Jul 1997 17:12:09 GMT  
 problems when initializing pointers to byte arrays

<snip>
|> If I want to initialize a pointer to point to a string array I write:
|>
|>      char *s3  = {"abc"};
|>
|> Now if I want to do the same with 2nd example above then I run into problems:
|>
|>      char *s4  = {'a', 'b', 'c'};
|>
|>
|> Apparently this line is illegal in C ?
|> All the compilers I tested are generating an error message.

A pointer has to point somewhere! In char*s3 = "abc", you are requesting the
compiler for an unmodifiable array of char containing the four characters 'a','b'
'c' and '\0', and set s3 to be the address of the first element of this array.

So, are you asking whether it is possible to have an unnamed array of 3
characters 'a','b' and 'c'? No. Why do you need this anyway? Won't a named
modifiable array equally well?

char s4arr[3] = "abc", *s4 = s4arr;

|> Also the ANSI C-spec does not help me here either.

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87544-0285,USA H:#3,802,9 St,NM87545
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Sun, 27 Jul 1997 23:45:51 GMT  
 problems when initializing pointers to byte arrays

Quote:
> Is there any way to initializing a pointer to an byte array.
> In this example I used ASCII-characters, but I want to put in
> hex values in my real application which looks like this:
>      unsigned char *b[] = {12, 13, 14, 15, 16, 16, 17, 17, 18, 19, 20},

    Remove the "*" and this will work. Also, if you put a value into
    the square brackets, you can use a string and the trailing 0 won't
    be stored. For example...

        char    hello[5] = "hello";
        char    world[5] = "\x77\x6f\x72\x6c\x64";

Quote:
> Actually I want to set up a pointer array where each element points to
> a byte array.

    One approach is something like the following...

        unsigned char b5[] = { 1, 2, 3, 4, 5 };
        unsigned char b4[] = { 1, 2, 3, 4 };
        unsigned char c5[] = { 9, 8, 7, 6, 5 };
        unsigned char *p[] = { b5, b4, c5, NULL };

    ...where p[] is your pointer array.

Quote:
> Jurgen

--

***  "Radioactivity - It's in the air, for you and for me" - Kraftwerk  ***


Mon, 28 Jul 1997 18:17:38 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. problems when initializing pointers to byte arrays

2. Initializing an array of pointers through a function

3. Initializing an array of pointers.

4. initializing array of pointers to int

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

6. Initialized array of functions pointers in a class??

7. FAQ: Initializing character array problems

8. array Initialize problem

9. turbo C initializing pointer problem

10. vtable pointer problem with objects initialized within DLL

11. converting an array of bytes ( BYTE* ) to a BITMAP struct

12. initialize a pointer to pointer

 

 
Powered by phpBB® Forum Software