By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,116 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,116 IT Pros & Developers. It's quick & easy.

Using sizeof() with a pointer : Not the normal debate....

P: n/a
My Google Groups search on this one showed the endless debates and I
did my best to look through them to see if my question was answered
and didn't find it. So, here it goes....

From the GG search, I have pretty much gathered and understand why
using sizeof(pointer to char[x]) returns 4 in my program. Is there a
way to get it to return the actual amount of characters?

the basic idea is that I am initializing a char [50] and passing it
into a function that accepts a char*. That function then passes it
into an API as a char* and loads it up with characters. In this case
it is 11 characters long.

As we know.... doing a sizeof() inside of the function that has the
pointer to the original char[50] returns 4. Is there a way to get it
to return the amount of characters (in this case 11)?

Code <clipped>:

//***********

memcpy(inBuffer, _dev.pDeviceType, sizeof(_dev.pDeviceType));

// This returns the first 4 characters out of the 11 total.
// My work around for now....

sprintf(inBuffer, "%s", _dev.pDeviceType);

//***********
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
What is _dev.pDeviceType? Is it null terminated string? If yes, use
strlen() instead.
Size of pointer is 4 byte on 32 bit system, so following will copy only
4 byte.
memcpy(inBuffer, _dev.pDeviceType, sizeof(_dev.pDeviceType));
That function then passes it
into an API as a char* and loads it up with characters. In this case
it is 11 characters long.

How come to know it? Does it mean API returns the No of characters? If
yes, then You may use that in memcpy() .

Jul 23 '05 #2

P: n/a
Charlie wrote:
My Google Groups search on this one showed the endless debates and I
did my best to look through them to see if my question was answered
and didn't find it. So, here it goes....

From the GG search, I have pretty much gathered and understand why
using sizeof(pointer to char[x]) returns 4 in my program. Is there a
way to get it to return the actual amount of characters?

the basic idea is that I am initializing a char [50] and passing it
into a function that accepts a char*. That function then passes it
into an API as a char* and loads it up with characters. In this case
it is 11 characters long.

As we know.... doing a sizeof() inside of the function that has the
pointer to the original char[50] returns 4. Is there a way to get it
to return the amount of characters (in this case 11)?
strlen() + 1 // if you initialized everything correctly

Code <clipped>:

//***********

memcpy(inBuffer, _dev.pDeviceType, sizeof(_dev.pDeviceType));

// This returns the first 4 characters out of the 11 total.
// My work around for now....

sprintf(inBuffer, "%s", _dev.pDeviceType);

//***********


Jul 23 '05 #3

P: n/a
Charlie schreef:
My Google Groups search on this one showed the endless debates and I
did my best to look through them to see if my question was answered
and didn't find it. So, here it goes....

From the GG search, I have pretty much gathered and understand why
using sizeof(pointer to char[x]) returns 4 in my program. Is there a
way to get it to return the actual amount of characters?
No, because there is no meaning to the word "actual"
the basic idea is that I am initializing a char [50] and passing it
into a function that accepts a char*. That function then passes it
into an API as a char* and loads it up with characters. In this case
it is 11 characters long.

As we know.... doing a sizeof() inside of the function that has the
pointer to the original char[50] returns 4. Is there a way to get it
to return the amount of characters (in this case 11)?


No. C++ has no way of knowing - unless there is something special
about these bytes. e.g. the first 10 are not 0 and the 11th is 0.
In that case, we call the bytes a string (aka "C string"). You can
then use strlen.

Other byte sequences have a convention in which the first few bytes
indicate the length that follows. E.g. some Windows APIs use this
convention.

Now, since there are at least two ways to encode the sequence length
in a sequence, what should C++ do? It doesn't know which way was
used. You do, so you should write the code.

HTH,
Michiel Salters

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.