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

C

Charlie

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);

//***********
 
U

upashu2

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.How come to know it? Does it mean API returns the No of characters? If
yes, then You may use that in memcpy() .
 
M

Mercator

Charlie said:
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
 
M

msalters

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top