Identifying char * vs char[CONSTANT]

E

eyal.susser

Hi,

I'm curious. I've seen code with methods that can somehow tell a
char* from a char[CONSTANT]. How? I've tried using a template like
this

template <size_t size>
void & discriminate(char s[size])
{
cout << "Static string: " << sizeof(s) << endl;

return *this;
}

template <typename TYPE>
void & discriminate(TYPE * s)
{
cout << "Pointer: " << sizeof(s) << endl;

return *this;
}

to no avail. My code jumps to the second discriminate.

Thanks.
 
E

eyal.susser

I made some sloppy pastes before, ignore the return types
:)
Also, if I write

void discriminate(char s[])
{
cout << "Static string: " << sizeof(s) << endl;
}

I enter the right method, but the sizeof(s) is always 4 (sizeof(char*))
instead of CONSTANT.

Thanks
 
V

Victor Bazarov

I'm curious. I've seen code with methods that can somehow tell a
char* from a char[CONSTANT]. How? I've tried using a template like
this

template <size_t size>
void & discriminate(char s[size])

There are no references to void, IIRC, so you can't say 'void &'. But
you can say

{
cout << "Static string: " << sizeof(s) << endl;

return *this;

Huh? Convertible to void &? [shaking his head]
}

template <typename TYPE>
void & discriminate(TYPE * s)
{
cout << "Pointer: " << sizeof(s) << endl;

return *this;
}

to no avail. My code jumps to the second discriminate.

Try it again. Without void& this time.

V
 
E

eyal.susser

Like I said in my second post, I made some sloppy pastes. The void
thing has nothing to do with anything. Here is a new version of the
snippets:

template <size_t size>
void & discriminate(char s[size])
{
cout << "Static string: " << sizeof(s) << endl;
}

template <typename TYPE>
void & discriminate(TYPE * s)
{
cout << "Pointer: " << sizeof(s) << endl;
}

void discriminate(char s[])
{
cout << "Static string: " << sizeof(s) << endl;

}

Only the third one really identifies a char s[10] parameter I pass, and
even then, sizeof(s) is 4 which is a pointer size, not the size of the
static array I passed.

Thanks
 
R

Ron Natalie

Hi,

I'm curious. I've seen code with methods that can somehow tell a
char* from a char[CONSTANT]. How? I've tried using a template like
this

template <size_t size>
void & discriminate(char s[size])

This function has the signature char*s. Arrays in function arguments
are mapped to pointers by definition. You need to twart it with a
reference:
void& discriminate(char (&s)[size])
 
E

eyal.susser

Dude! Thanks! I thought the a by ref might do it, but couldn't quite
get it to compile...
 
V

Victor Bazarov

Ron said:
Hi,

I'm curious. I've seen code with methods that can somehow tell a
char* from a char[CONSTANT]. How? I've tried using a template like
this

template <size_t size>
void & discriminate(char s[size])


This function has the signature char*s. Arrays in function arguments
are mapped to pointers by definition. You need to twart it with a
reference:
void& discriminate(char (&s)[size])

In all this, what's "void&"? I thought references to void aren't
allowed. Am I wrong?

V
 
I

Ioannis Vranos

Like I said in my second post, I made some sloppy pastes. The void
thing has nothing to do with anything. Here is a new version of the
snippets:

template <size_t size>
void & discriminate(char s[size])

You can't do that!


Also you can't dissociate between a char array and a char * as arguments in another
function. They are both char * even in the case:


void somefunc(char array[4]);


array is *still* considered a pointer when declared as function argument, you can
increment and decrement it (the 4 has no effect), along with all its variations:

void somefunc(char array[]);

void somefunc(char *array);
 
A

Alf P. Steinbach

* (e-mail address removed):
Dude! Thanks! I thought the a by ref might do it, but couldn't quite
get it to compile...

I always hate when people do that to me, so (although Ron is _very_ helpful
in this group) credit for solving your problem first should go to Victor,
who you replied to without recognizing that he provided the same solution.
 

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,294
Messages
2,571,511
Members
48,206
Latest member
EpifaniaMc

Latest Threads

Top