strings

J

JKop

alex posted:
what data type is a line like this:

"adslfkj adsfjk adf"

It's an expression of type char* , which points to the first in an array of
characters.


-JKop
 
A

Alf P. Steinbach

* "alex said:
what data type is a line like this:

"adslfkj adsfjk adf"

It is an array of n constant chars. Which in some but not all contexts
decays to a pointer to constant char. And in some but not all contexts,
as backward compatibility with C, decays to pointer to plain char.
 
A

alex

thanks alf

Alf P. Steinbach said:
It is an array of n constant chars. Which in some but not all contexts
decays to a pointer to constant char. And in some but not all contexts,
as backward compatibility with C, decays to pointer to plain char.

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

Jorge Rivera

alex said:
what data type is a line like this:

"adslfkj adsfjk adf"

This is also considered a 'string literal'. As well pointed out, you
can use this as a const char* or a char[].

JLR
 
R

Rolf Magnus

JKop said:
alex posted:


It's an expression of type char* , which points to the first in an
array of characters.

No. It's of type array of const char with 19 elements.
You can see the difference if you look at sizeof(char*) vs.
sizeof("adslfkj adsfjk adf").
 
J

JKop

Rolf Magnus posted:
No. It's of type array of const char with 19 elements.
You can see the difference if you look at sizeof(char*) vs.
sizeof("adslfkj adsfjk adf").

Seeing as you are right, look at this.


void Chocolate(char monkey[19])
{
;
}

void Chocolate(char* monkey)
{
;
}


int main(void)
{
Chocolate("adslfkj adsfjk adf");

return 0;
}



If char* and char[19] are two different types, why they hell won't the
above compile! I wish C++ would make it's mind up and determine the type of:

"adslfkj adsfjk adf"



....How does sizeof() work? Does it generate a figure based upon the type of
the object passed to it?


-JKop
 
P

Phlip

JKop said:
void Chocolate(char monkey[19])
{
;
}

void Chocolate(char* monkey)
{
;
}


int main(void)
{
Chocolate("adslfkj adsfjk adf");

return 0;
}

If char* and char[19] are two different types, why they hell won't the
above compile! I wish C++ would make it's mind up and determine the type
of:

They won't compile because an array in a function prototype decays to a
pointer. Then either pointers or arrays, passed in, decay too. But the
compiler sees to ambiguous signatures at lookup time.

C++ is not strictly a typesafe language. But references are more typesafe
than copies, so...

void Chocolate( char (&monkey) [19] )
{
assert(19 == sizeof monkey);
}
 
R

Rolf Magnus

JKop said:
Rolf Magnus posted:
No. It's of type array of const char with 19 elements.
You can see the difference if you look at sizeof(char*) vs.
sizeof("adslfkj adsfjk adf").

Seeing as you are right, look at this.


void Chocolate(char monkey[19])
{
;
}

void Chocolate(char* monkey)
{
;
}


int main(void)
{
Chocolate("adslfkj adsfjk adf");

return 0;
}



If char* and char[19] are two different types, why they hell
won't the above compile!

This is a quirk of the C++ language. In the first function, 'monkey' is
_not_ of type char[19], but rather of type char*.
I wish C++ would make it's mind up and determine the type of:

"adslfkj adsfjk adf"

The type of "adslfkj adsfjk adf" is still const char[19].
...How does sizeof() work? Does it generate a figure based upon the
type of the object passed to it?

It returns the number of storage bytes that the object needs.
 

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,169
Messages
2,570,920
Members
47,462
Latest member
ChanaLipsc

Latest Threads

Top