Need help with arrays 
Author Message
 Need help with arrays

Quote:

> And I want to put the segment and the offset of Buf into segm and offs (in
> order to use ASM code after).

union {
        char far *ptr;
        struct {
                int off;
                int seg;
        } b;

Quote:
} a;

a.ptr = (char far *)(buf);

inspect a.b.off for offset and a.b.seg for segment

WARNING: I have not tried this out since Turbo C 2.0; so
no warranty;

regards,
        V.



Tue, 21 Sep 1999 03:00:00 GMT  
 Need help with arrays

Quote:

>     Does everyone know how to get the address (segment and offset) of an
> array in C/C++.

The form of the memory address is system dependent. You don't even
say what hardware you are on. Maybe try a news group that relates
to your system.

--
Standard disclaimers apply.
I don't buy from people who advertise by e-mail.
I don't buy from their ISPs.
Dan Evens



Tue, 21 Sep 1999 03:00:00 GMT  
 Need help with arrays

Quote:

> Hi!
>     Does everyone know how to get the address (segment and offset) of an
> array in C/C++.

> There is my code:
> unsigned char MyFunc() {
> unsigned char buf[256]
> unsigned int segm,offs

> And I want to put the segment and the offset of Buf into segm and offs (in
> order to use ASM code after).

> Can anybody help me? Thanx!

First, you should know that the name of you array alone is the same as

array[0].

(i.e char my_array[50], my_array = my_array[0]). The name alone is the

address of the first element.

To know the offset you can just use the name of the array.

But I will give you the entire code that will work with all array.

begin:

                #include <dos.h>

                unsigned int segment, offset;           /* 16bit seg:off */

                TYPE    array[number_of_element];       /* your array of type TYPE (can be a

structure) */

                segment = FP_SEG((type *)array);        /* this will give you the segment */

                offset  = FP_OFF((type *)array);        /* this will give you the offset  */

end.

Now a example with your array (i.e char buf[256])

begin:

                #include <dos.h>

                unsigned int segment, offset;           /* 16bit seg:off */

                char    array[256];                     /* your array of type char (can be a structure) */

                segment = FP_SEG((char *)array);        /* this will give you the segment */

                offset  = FP_OFF((char *)array);        /* this will give you the offset  */

end.

Note here, function FP_SEG and FP_OFF take for argument pointer to void

far*. So when you call this function,

don't forget to 'override' the type (i.e (char *) or (char far *) or

(type *) or ...).

I hope you have all the information. If you still have question, don't

hesitate, ask me



Thu, 23 Sep 1999 04:00:00 GMT  
 Need help with arrays

Quote:


>> Hi!
>>     Does everyone know how to get the address (segment and offset) of an
>> array in C/C++.

>> There is my code:
>> unsigned char MyFunc() {
>> unsigned char buf[256]
>> unsigned int segm,offs

>> And I want to put the segment and the offset of Buf into segm and offs (in
>> order to use ASM code after).

>> Can anybody help me? Thanx!

>First, you should know that the name of you array alone is the same as
>array[0].
>(i.e char my_array[50], my_array = my_array[0]). The name alone is the
>address of the first element.

Not true - they aren't even of the same type.  In many circumstances,
the un{*filter*}erated name (my_array) decays into a pointer equivalent to
&(my_array [0]).  It is never the same as my_array [0].

[DOS specific code snipped - please fix your posting software to stop
 inserting alternate blank lines.]

John
(Newsgroups trimmed.)
--
John Winters.  Wallingford, Oxon, England.



Fri, 24 Sep 1999 03:00:00 GMT  
 Need help with arrays

Quote:

>First, you should know that the name of you array alone is the same as

>array[0].

>(i.e char my_array[50], my_array = my_array[0]). The name alone is the

>address of the first element.

Really?

If that is the case, shouldn't   sizeof my_array   be the same value
as  sizeof(char *)?

I think that what you are trying to say is that an expression whose type
is array of <type> yields a value that is pointer to <type>
in contexts where that expression is evaluated.

In comp.lang.c, schoolyard answers and explanations don't suffice.

Quote:
>To know the offset you can just use the name of the array.

>But I will give you the entire code that will work with all array.

>begin:

>            #include <dos.h>

This is not a standard C header file.  Segments and offsets are artifacts
of particular implementations of the C language.


Sun, 26 Sep 1999 03:00:00 GMT  
 Need help with arrays

Quote:


> > Hi!

> >     Does everyone know how to get the address (segment and offset) of an
> > array in C/C++.

> First, you should know that the name of you array alone is the same as
> array[0].

> (i.e char my_array[50], my_array = my_array[0]). The name alone is the
> address of the first element.

This is somewhat sloppy and partially wrong: in your example "array" is
the
same as "&array[0]". The "&" operator is *very* important in this
context.

Quote:

> To know the offset you can just use the name of the array.
> But I will give you the entire code that will work with all array.

> begin:

>                 #include <dos.h>
>                 unsigned int segment, offset;           /* 16bit seg:off */
>                 TYPE    array[number_of_element];       /* your array of type TYPE (can be a
> structure) */
>                 segment = FP_SEG((type *)array);        /* this will give you the segment */
>                 offset  = FP_OFF((type *)array);        /* this will give you the offset  */
> end.

Your example code is very system specific. It will most probably work
for only one specific compiler. The header <dos.h> is NOT a standard C
header.

[rest of INTEL specific advice snipped]

Stephan



Sun, 26 Sep 1999 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Need help with arrays

2. Need help Sorting Arrays with an insert function.

3. Need help with Arrays: Specific program question

4. need help with arrays please

5. need help with array of string pointers

6. Need help: Passing array as parameter.

7. need help With Arrays

8. Need Help with array...Please Advice

9. Aarghh! I need help with array of structs

10. Need help with array's

11. Need help with array of pointers to structure

12. Need help with array realloc's

 

 
Powered by phpBB® Forum Software