473,324 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Identifying char * vs char[CONSTANT]

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.

Jul 23 '05 #1
8 1668
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

Jul 23 '05 #2
ey*********@gmail.com wrote:
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

template<size_t size>
void discriminate(char (&s)[size])
{
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
Jul 23 '05 #3
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

Jul 23 '05 #4
ey*********@gmail.com wrote:
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])
Jul 23 '05 #5
Dude! Thanks! I thought the a by ref might do it, but couldn't quite
get it to compile...

Jul 23 '05 #6
Ron Natalie wrote:
ey*********@gmail.com wrote:
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
Jul 23 '05 #7
ey*********@gmail.com wrote:
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);
--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #8
* ey*********@gmail.com:
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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sathyaish | last post by:
In trying to replace character literals for their char constant, I am having difficulty printing the char constant for backslash. It instead prints the char literal. How do I resovle this? ...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
6
by: vb | last post by:
Hi all, I ran the follwing program on windows-XP(86 architecture) using gcc. #include <stdio.h> int main() { char ch='a'; printf("%u %u\n",sizeof(ch),sizeof('a')); return 0;
2
by: Cosmin Prund | last post by:
This is supposed to be very easy, only I did not find this in MSDN so I'll ask here: How do I enter a char constant using the char's ASCII number? Specifically I want to be able to compare a...
15
by: Alok Kumar | last post by:
#include <string.h> void myfn() { char a = 'A'; char b; strcpy(b, &a); } Would I always get 'A' in b and '\0' in b after the strcpy?
1
by: svlsr2000 | last post by:
Here is a small program which i saw a couple of years ago. #include<stdio.h> int main() { char x; if(sizeof(int)==sizeof('a')) printf("\n C compiler\n"); ...
20
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
6
by: comp.lang.c++ | last post by:
this is a sample example about this question #include<stdio.h> void chg(char* t) { char *s=t; char p=*t; while(*t++=*++s); *--t=p; } int main()
3
by: plf | last post by:
Hi, The problem that I am faced with in the following code is that the two character constant arrays "a" and "b" end up containing the same data. For example, in "void myvar::getvar( char const...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.