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