placing an array at a specific mem location

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

placing an array at a specific mem location

424 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ECamino on Wed Feb 27 15:11:50 MST 2013
Hi,

I have an api call that returns a pointer and reserves 8 bytes of data.  I later need that pointer for another api call.  But I need to use the reserved data as an array and so I need a way to make an array be at the address given by the pointer.  How do I put the array at the pointer location?

uint8_t * ptr;
uint8_t array[8];

ptr = api_call();
// array = api_call(); doesnt work

// put array[] at address returned by ptr...
// populate array[]

api2_call(ptr)

Thanks,

EC
0 Kudos
8 Replies

417 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ECamino on Thu Feb 28 14:46:50 MST 2013
Yes, Zero!  That's a more elegant way to do it.

Thanks,

EC
0 Kudos

417 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Thu Feb 28 13:00:11 MST 2013
And with a struct and memcopy it looks good also

uint8_t * ptr;

typedef struct{
 uint8_t x;
 uint8_t y;
}struct_position;  //struct to enable memcopy of a block
struct_position position;

ptr = api_call();

(more code)

memcpy(ptr, &position, (sizeof(struct_position)));

(more)

pUsbApi->hw->WriteEP(hUsb, 0x82, ptr, 8);
0 Kudos

417 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ECamino on Thu Feb 28 11:29:33 MST 2013
Thanks wrightflyer!

I did find a way that works great:

uint8_t * ptr;

ptr = api_call();

(more code)

ptr[0] = x;
ptr[1] = y;
0 Kudos

417 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by wrighflyer on Thu Feb 28 10:20:48 MST 2013
I don't understand why you need array[] at all. ptr is the base of an array. You can use:
ptr = api_call();
uint8_t val = ptr[3];

When you write ptr[3] in C it is immediately broken down as if you had written *(ptr + 3). Array accessing syntax [N] is just syntactic sugar for pointer access anyway.
0 Kudos

417 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ECamino on Thu Feb 28 08:03:55 MST 2013
If I did something like this:

#include <stdio.h>

int main() {
char buff[8] = "8 Bytes!";
char * pdata;
memcpy (pdata, &buff[0], sizeof (pdata));
}

pdata would point to a copy of the data. But I would have to use more ram (still using two bufferes), and I'd use more program memory (and probably more ram) for the call and running the func...:(
0 Kudos

417 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Thu Feb 28 07:40:58 MST 2013
:confused:

Why don't you just handle your buffers with memcpy :confused:
0 Kudos

417 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ECamino on Thu Feb 28 07:35:38 MST 2013
-
0 Kudos

417 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ex-kayoda on Wed Feb 27 16:51:43 MST 2013

Quote: ECamino
... I need a way to make an array be at the address given by the pointer



That's not possible. You can't change declaration address of your array. Is there a special reason why you don't use the pointer (like the rest of the world) :confused:
0 Kudos