Long as array/pointer address 
Author Message
 Long as array/pointer address

I'd like to do the following:

  long index = 10;;
  char *string = "This is a test string";

  printf("%c\n", string[index]);
  printf("%c\n", *(string + index));

but when I compile the above in a 16bit compiler (haven't tried it on a
32bit compiler), the compiler apparently casts the index to int in the
two printf lines.  Are array/pointer indexes int according to the ANSI
standard?

Any way to access arrays with long indexes?  e.g.  string[66000]?
--



Thu, 28 Aug 1997 07:58:00 GMT  
 Long as array/pointer address

Quote:
> I'd like to do the following:
>   long index = 10;;
>   char *string = "This is a test string";
>   printf("%c\n", string[index]);
>   printf("%c\n", *(string + index));
> but when I compile the above in a 16bit compiler (haven't tried it on a
> 32bit compiler), the compiler apparently casts the index to int in the
> two printf lines.  Are array/pointer indexes int according to the ANSI
> standard?

The standard allows any integral type.  However, the compiler is allowed to
use magic.  In many 16 bit implementations objects larger than can be
indexed with a 16 bit integer are not supported or are supported only in
some non-standard way (e.g., far in most PC compilers).  If the compiler knows
that the object cannot be large enough to need a long for indexing it may
optimize by converting to int (or, for that matter, to char) when it does
the calculation.

--
Mike Rubenstein



Thu, 04 Sep 1997 09:03:22 GMT  
 Long as array/pointer address
 -=> Quoting Rui M. Pereira to All <=-

 RMP> but when I compile the above in a 16bit compiler (haven't tried it on
 RMP> a 32bit compiler), the compiler apparently casts the index to int in
 RMP> the two printf lines.  Are array/pointer indexes int according to the
 RMP> ANSI standard?

 RMP> Any way to access arrays with long indexes?  e.g.  string[66000]?

The problem is not that the index is cast to an int, the problem is that
on a 16bit machine, POINTERS ARE 16 BITS!  The only ways to do it are
1) declare the pointers as far and do segment manipulation
2) declare the pointers as huge

Follow-ups to comp.os.msdos.programmers

                                Just a Thought,
                                Jim Trigg

... If it wasn't for C, we would be using BASI, PASAL and OBOL!
___ Blue Wave/QWK v2.12



Sun, 07 Sep 1997 07:59:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. struggling all day long: passing by address array of structures

2. address of structure == address of first member? (long)

3. Using values of one array as pointer counters to access data in another ( long )

4. array indexing by pointer and long index

5. assign address to pointer array

6. passing the address of an array of pointers?

7. Pointers & incrementing array address

8. char size (was long long long long long int)

9. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

10. Array of pointers, pointer to array...

11. array pointer/pointer array

12. arrays pointers array of pointers

 

 
Powered by phpBB® Forum Software