14

I want to pass a pointer to a function. I want this pointer to point to some place in the middle of an array. Say I have an array like such unsigned char BufferData[5000];, would the following statement be correct syntactically?

writeSECTOR( destAddress, (char *)( BufferData + (int)(i * 512 )) );
// destAddress is of type unsigned long
// writeSECTOR prototype: int writeSECTOR ( unsigned long a, char * p );
// i is an int
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
PICyourBrain
  • 9,976
  • 26
  • 91
  • 136
  • 1
    Yes. The cast to `int` is unnecessary but otherwise your statement is valid. – PP. Oct 06 '10 at 13:17
  • 2
    In fact I would argue your solution is better than many of the proposed answers below! – PP. Oct 06 '10 at 13:18
  • I prefer the &array[i] approach as its more obvious that you're offsetting a pointer into an array, i.e. BufferData+(i*512) is less readable about its intentions than &BufferData[i*512]. – Cthutu Oct 06 '10 at 13:23
  • 1
    @Cthutu I disagree. `array + i` is far more readable than the `&array[i]` because I read the latter as "dereference array, element i, and take the address" as opposed to the simpler "address offset i from array". The problem with your favourite notation is that it is switching between pointers and de-referencing in the one statement. Whereas pointer addition is just pure math. – PP. Oct 06 '10 at 13:46
  • You're not really because the compiler will produce the same code. But it's really a subjective thing. When I see array brackets I know its a pointer to an array and think about ranges. – Cthutu Oct 07 '10 at 10:54

7 Answers7

19

That would do, but just make it:

 writeSECTOR( destAddress, &BufferData[i * 512]);

(It sounds like writeSECTOR really should take an unsigned char* though)

nos
  • 223,662
  • 58
  • 417
  • 506
9

Pointer arithmetic is fairly simple to understand. If you have a pointer to the first element of an array then p + 1 points to the second element and so on regardless of size of each element. So even if you had an array of ints, or an arbitrary structure MyData it would hold true.

MyData data[100];
MyData *p1 = data;;  // same as &data[0]
MyData *p2 = p1 + 1; // same as &data[1]
MyData *p3 = p2 + 1; // same as &data[2]
MyData *p4 = p2 - 1; // same as &data[0] again

If your array is unsigned char then you just add however many bytes offset you wish to go into the array, e.g.

unsigned char data[16384];
unsigned char *offset = data + 512; // same as &data[512]
*offset = 5; // same as data[512] = 5;

Alternatively if the notation is confusing, you can always refer to it as it is shown in comments above, e.g. &data[512]

locka
  • 5,809
  • 3
  • 33
  • 38
8

You can just do BufferData + i * 512. An arithmetic + operator on char* yields a char* when you add an integer value to it.

reko_t
  • 55,302
  • 10
  • 87
  • 77
3

That should work. In C, adding an integer to a pointer increases the pointer by the integer multiplied by the sizeof the type the pointer points to.

Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
0

it looks ok, but try it on the compiler,

You can use writeSECTOR( destAddress, &BufferData[i * 512] );

Jose
  • 41
  • 2
0

That looks like it will pass in the address of the i*512'th element of the array. Is that what you want it to do?

I don't really see what that cast to int is buying you though.

T.E.D.
  • 44,016
  • 10
  • 73
  • 134
0

That should work as you think. Pointer in C is just an address in RAM,so you could drift the pointer in your measure.

shenju
  • 11
  • 1