Get the length of data a pointer is pointing to

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have code like this:

unsigned char *buf
*buf=1234567;

How I detect the the size of data buf is pointing to? (In this case is 7)

Thanks
Tran Hong Quang
 
I have code like this:

unsigned char *buf
*buf=1234567;

How I detect the the size of data buf is pointing to? (In this case is 7)

there is a mistake in your code. buf is a pointer, and you assign it a value
of 1234567.
this means that 1234567 is used as an address, not as data. you should get
compiler warnings for this.

you should use
*buf="1234567";
instead.

this will cause buf to point to a string.
you can then use strlen(buf); to determine the length.
please note that this only gets you the length of the string.
if you have something like this

char mybuf[100];
char* buf = mybuf;

there is no way to get the size of that buffer by just using buf.

there are 2 things you can do:
you have to know in advance how large it will be (for example you have a
BUF_SIZE constant somewhere) or you have to get a size value from somewhere
else when mybuf is assigned to buf.

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
Back
Top