how to modify a pointer to an array one sizeof(int) 
Author Message
 how to modify a pointer to an array one sizeof(int)

My question is under the following situation:
          struct {
              float a;
              float b;
              .....
              float n;
           } *ay;

Inside the memory, there stored  a series of number of type:
            add     value
     -----------------------------
             0000   int
             0002   float
             0006   float
             0010   float
              ...       ...
             XXXX   float
The array named ay is somehow pointed to 0000, i.e. ay = 0000;
How can I modify the pointer to the array ay to 0002, i.e. let
ay = 0002, so that I can use ay[0].a to access the first float number at
0002, and so on.

Sent via Deja.com http://www.*-*-*.com/
Share what you know. Learn what you don't.



Fri, 07 Dec 2001 03:00:00 GMT  
 how to modify a pointer to an array one sizeof(int)

Quote:

>My question is under the following situation:
>          struct {
>              float a;
>              float b;
>              .....
>              float n;
>           } *ay;

>Inside the memory, there stored  a series of number of type:
>            add     value
>     -----------------------------
>             0000   int
>             0002   float
>             0006   float
>             0010   float
>              ...       ...
>             XXXX   float
>The array named ay is somehow pointed to 0000, i.e. ay = 0000;
>How can I modify the pointer to the array ay to 0002, i.e. let
>ay = 0002, so that I can use ay[0].a to access the first float number at
>0002, and so on.

This slightly looks like homework, hmmmm

I will only say that the solution to your problem involves a cast.



Fri, 07 Dec 2001 03:00:00 GMT  
 how to modify a pointer to an array one sizeof(int)

Quote:

>My question is under the following situation:
>          struct {
>              float a;
>              float b;
>              .....
>              float n;
>           } *ay;

>Inside the memory, there stored  a series of number of type:
>            add     value
>     -----------------------------
>             0000   int
>             0002   float
>             0006   float
>             0010   float
>              ...       ...
>             XXXX   float
>The array named ay is somehow pointed to 0000, i.e. ay = 0000;

ay isn't an array, it is a pointer. Speciifc memory addresses such as thing
are fairly meaningless in standard C, there is no standard mechanism
to set a pointer to such an address (since how addresses are formed and
interpreted is inherently platform-specific). You need to check your
compiler documentation as to if and how it supports it. A common approach
is to cast an integer value to a pointer type but don't bet on it or
how the integer values are interpreted.

Quote:
>How can I modify the pointer to the array ay to 0002, i.e. let
>ay = 0002, so that I can use ay[0].a to access the first float number at
>0002, and so on.

Another issue is alignment. On many platforms 2 as an address isn't
correctly aligned to access a float. Such an access might cause the
program to crash.

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


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



Fri, 07 Dec 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. int pointer to int array

2. relation between sizeof(void*), sizeof(int)

3. sizeof(int) sizeof(long) etc.

4. sizeof and arrays/pointers question

5. sizeof an array referenced by pointer

6. Sorting array of pointers to int

7. pointer to int array question

8. passing of pointers to int arrays?

9. Returning Int array pointers from functions

10. initializing array of pointers to int

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

12. sizeof pointer to pointer

 

 
Powered by phpBB® Forum Software