Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > String Problem?

Reply
Thread Tools

String Problem?

 
 
Keith Thompson
Guest
Posts: n/a
 
      10-08-2005
Dale <> writes:
> Mark McIntyre <> wrote in
> news::
>> On 07 Oct 2005 17:53:50 GMT, in comp.lang.c , Dale <>
>> wrote:
>>>"Davy" <> wrote in news:1128661915.425811.133260
>>>@g44g2000cwa.googlegroups.com:
>>>>
>>>> char x[4]={"my"};
>>>
>>>Remember. sweetums, 'x' is a pointer when you declare it that way.

>>
>> Actually, no, this is terribly terribly wrong. In neither declaration
>> is 'x' a pointer - its an array[4] of char.

>
> You're the one who's wrong, here, ****nut -- 'x' is a character pointer
> when you declare it that way. That's how C stores an array in memory;
> i.e., as a pointer to a chunk of RAM.


Dale, let me offer some friendly advice.

First, don't be rude.

Second, don't be simultaneously rude and wrong.

Arrays are not pointers. Pointers are not arrays. An array is not
stored in memory as "a pointer to a chunk of RAM", it's stored in
memory as an array.

The declaration
char x[4];
declares an array object, with or without an initialization. It does
not declare a pointer object.

I think what's confused you is the fact that an array name, or any
expression of an array type, is implicitly converted to a pointer to
the array's first element in most contexts. (The exceptions are the
operand of the unary "&" or "sizeof" operator, and a string literal in
an initializer.)

> Run this little program and observe the output:
>
> int main(void)
> {
>
> char x[4]={"my"};
>
> printf("%c\n", x);
> printf("%c\n", *x);
>
> return 0;
> }
>
> The first printf() will display garbage (because it's actually printing n
> the value of a pointer) but the second will display the letter 'm'
> (because it's printing the value that the pointer points to). If 'x'
> were not a pointer then this wouldn't even ****ing compile because '*x'
> would be considered invalid indirection.


The first printf() invokes undefined behavior. The expression x,
which is of array type, is implicitly converted to a pointer value
of type char*. Passing this as an argument to printf() with a "%c"
format invokes undefined behavior. It happens to display garbage,
but since you're lying to the compiler it could do anything.

In the second printf(), the subexpression x is again converted to
a value of type char*. Dereferencing this pointer value yields the
character value 'm', so you're right about this one.

And of course you're missing the require "#include <stdio.h>, so
*any* call to printf() invokes undefined behavior. On many (most?)
implementations, it happens to work anyway, but you shouldn't count
on that.

The relationship between arrays and pointers is a common source of
confusion among C newbies. You need to read section 6, "Arrays and
Pointers", of the C FAQ (google "C FAQ" to find it). I see that this
thread is cross-posted to comp.lang.c and comp.lang.c++ (which is
rarely a good idea), but this happens to be an area where C and C++
are similar.

> Dumbass.


There's a good chance that a lot of the regulars in these newsgroups
have already killfiled you, or will do so as soon as they see your
article. You may already have permanently damaged your ability to
participate here and to benefit from the advice of experts.

Be embarrassed.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
 
Skarmander
Guest
Posts: n/a
 
      10-08-2005
Dale wrote:
> Mark McIntyre <> wrote in
> news::
>
>
>>On 07 Oct 2005 17:53:50 GMT, in comp.lang.c , Dale <>
>>wrote:
>>
>>>"Davy" <> wrote in news:1128661915.425811.133260
>>>@g44g2000cwa.googlegroups.com:
>>>
>>>> char x[4]={"my"};
>>>
>>>Remember. sweetums, 'x' is a pointer when you declare it that way.

>>
>>Actually, no, this is terribly terribly wrong. In neither declaration
>>is 'x' a pointer - its an array[4] of char.

>
>
> You're the one who's wrong, here, ****nut -- 'x' is a character pointer
> when you declare it that way. That's how C stores an array in memory;
> i.e., as a pointer to a chunk of RAM.
>

I'm afraid the ****nut is quite correct. A character pointer is not a
character array. An array can be implicitly converted to a pointer and
the [] construct can be applied to pointers, which is not the same thing.

If 'x' were only a character pointer, what memory is "my" being written
to? 'x' is a character array here. Go read the standard if you don't
believe it. Or read the FAQ. Specifically section 6:
http://www.eskimo.com/~scs/C-faq/s6.html

<snipped code where the implicit conversion from array to pointer is
demonstrated>
> Dumbass.


Pleased to meet you. Language aside, the courtesy of reading the FAQ of
a newsgroup you're posting to benefits all involved.

S.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      10-08-2005
Kai-Uwe Bux <> writes:
> Dale wrote:
>> Mark McIntyre <> wrote in
>> news::
>>> On 07 Oct 2005 17:53:50 GMT, in comp.lang.c , Dale <>
>>> wrote:
>>>>"Davy" <> wrote in news:1128661915.425811.133260
>>>>@g44g2000cwa.googlegroups.com:
>>>>>
>>>>> char x[4]={"my"};
>>>>
>>>>Remember. sweetums, 'x' is a pointer when you declare it that way.
>>>
>>> Actually, no, this is terribly terribly wrong. In neither declaration
>>> is 'x' a pointer - its an array[4] of char.

>>
>> You're the one who's wrong, here, ****nut -- 'x' is a character pointer
>> when you declare it that way. That's how C stores an array in memory;
>> i.e., as a pointer to a chunk of RAM.

>
> I see that you have set the follow-ups to comp.lang.c. However, as your
> original comment was posted in comp.lang.c++, too, be informed that, in the
> context of C++, you are not correct with respect to the type of 'x'.

[snip]
> Maybe, you are correct within the context of the C programming language,
> though.


No, he's equally wrong in C.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      10-08-2005
Dale wrote:
> Mark McIntyre <> wrote in
> news::
>
>>On 07 Oct 2005 17:53:50 GMT, in comp.lang.c , Dale <>
>>wrote:
>>
>>>"Davy" <> wrote in news:1128661915.425811.133260
>>>@g44g2000cwa.googlegroups.com:
>>>
>>>> char x[4]={"my"};
>>>
>>>Remember. sweetums, 'x' is a pointer when you declare it that way.

>>
>>Actually, no, this is terribly terribly wrong. In neither declaration
>>is 'x' a pointer - its an array[4] of char.

>
> You're the one who's wrong,


No is isn't.

> here, ****nut


why resort to abuse? All it does is make *you* look bad and probably
make at least some of the knowledgeable people around here plonk you
reducing the chance of you getting useful help when you need it.

> -- 'x' is a character pointer
> when you declare it that way.


No, x is an array of char. Try checking any decent book on C (or C++) or
the comp.lang.c FAQ or the standard or any of the times this has been
discussed before.

> That's how C stores an array in memory;
> i.e., as a pointer to a chunk of RAM.


No. If it was a pointer you could assign to x, but every conforming
compiler will diagnose it as an error.

> Run this little program and observe the output:
>
> int main(void)
> {
>
> char x[4]={"my"};
>
> printf("%c\n", x);
> printf("%c\n", *x);
>
> return 0;
> }
>
> The first printf() will display garbage (because it's actually printing
> the value of a pointer)


No, it might do anything because an array decays to a pointer to its
first element in this situation and that is not what you have said you
will print.

> but the second will display the letter 'm'
> (because it's printing the value that the pointer points to). If 'x'
> were not a pointer then this wouldn't even ****ing compile because '*x'
> would be considered invalid indirection.


No, it compiles because this is one of the many situations in which an
array will decay to a pointer to the first element.

> Dumbass.


That description would appear to apply to you, not Mark. I suggest you
try actually learning the language before claiming people are wrong and
insulting them.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-08-2005
Dale wrote:
> Mark McIntyre <> wrote in
> news::
>>On 07 Oct 2005 17:53:50 GMT, in comp.lang.c , Dale <>
>>wrote:
>>>"Davy" <> wrote in news:1128661915.425811.133260
>>>@g44g2000cwa.googlegroups.com:
>>>
>>>> char x[4]={"my"};
>>>
>>>Remember. sweetums, 'x' is a pointer when you declare it that way.

>>
>>Actually, no, this is terribly terribly wrong. In neither declaration
>>is 'x' a pointer - its an array[4] of char.

>
>
> You're the one who's wrong, here, ****nut -- 'x' is a character pointer
> when you declare it that way. That's how C stores an array in memory;
> i.e., as a pointer to a chunk of RAM.


Just because you are completely wrong does not mean that you must use
abusive language. Cursing at Mark will not make your cluelessness
suddenly appear to be enlightenment. In
char x[4] = {"x");
x is an array of char, not a pointer, and only the as yet still ignorant
think otherwise. When the ignorant loudly declare their ignorance to be
truth, as you did, that correctable ignorance is transformed into
stubborn and boorish stupidity.

>
> Run this little program and observe the output:


Don't bother.
To start with, the variadic function printf() requires a declaration in
scope. Dale, who properly signs himself below as "Dumbass", left this out.

>
> int main(void)
> {
>
> char x[4]={"my"};
>
> printf("%c\n", x);


As everyone but Dale who styles himself "Dumbass" knows, when an array
is passed as an argument, the value passed is a pointer to the beginning
of that array. Nothing constructive can be said about whether x is an
array or not can be said from a situation in which a pointer is passed
whether x is an array or a pointer. We can, however, say that anyone
who thinks he could pass either an array or a pointer to printf and
expect anything meaningful from the absurd use of the "%c" specifier
should not be giving lessons to anyone. He needs to be rereading the
more elementary parts of his C text instead.

> printf("%c\n", *x);
>
> return 0;
> }
>
> The first printf() will display garbage (because it's actually printing
> the value of a pointer) but the second will display the letter 'm'
> (because it's printing the value that the pointer points to). If 'x'
> were not a pointer then this wouldn't even ****ing compile because '*x'
> would be considered invalid indirection.


We already know that the above argument is garbage. What Dale perhaps
should look at (extra brackets retained)
is:
#include <stdio.h>

int main(void)
{

char x[] = { "The array named 'x' has this." };
char *y = { "The pointer named 'y' points to this." };

printf("The array x (\"%s\") has size %lu\n", x,
(unsigned long) sizeof(x));
printf("The pointer y (\"%s\") has size %lu\n", y,
(unsigned long) sizeof(y));

return 0;
}


[Output for this implementation]
The array x ("The array named 'x' has this.") has size 30
The pointer y ("The pointer named 'y' points to this.") has size 4

>
> Dumbass.


If you want to use a .sig, remember to place the .sig separator before it.


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      10-08-2005
Dale wrote:


> You're the one who's wrong, here, ****nut -- 'x' is a character
> pointer when you declare it that way. That's how C stores an array
> in memory; i.e., as a pointer to a chunk of RAM.



Besides being dead wrong, you're a jerk. So it's a quick plonk for you.



Brian
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      10-08-2005
On 08 Oct 2005 19:26:59 GMT, in comp.lang.c , Dale <>
wrote:

>Mark McIntyre <> wrote in
>news: :
>
>> Actually, no, this is terribly terribly wrong. In neither declaration
>> is 'x' a pointer - its an array[4] of char.

>
>You're the one who's wrong, here, ****nut


You're amusingly wrong, as well as abusive. Arrays are not pointers.

>Run this little program and observe the output:


(program which passes an array and its first element to printf())

Which proves nothing. When passed to a function, an array argument
decays into a pointer to its first element. Please read the FAQ (6.3
onwards I believe) and the ISO standard if you need more detail,

Anyway, if we're writing test cases, try this one:

#include <stdio.h>
int main(void)
{
int x[365];
return printf("%d %d\n", sizeof(x), sizeof(*x));
}

And then ask yourself how large the pointers are on your system...

>The first printf() will display garbage (because it's actually printing
>the value of a pointer)


well, actually it prints garbage because %c and array[4] of int are
incompatible, and you've invoked undefined behaviour.

>Dumbass.


Physician, heal thyself.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      10-08-2005
On Sat, 08 Oct 2005 22:32:20 +0200, in comp.lang.c , Skarmander
<> wrote:

>Dale wrote:
>> Mark McIntyre <> wrote in
>>>Actually, no, this is terribly terribly wrong. In neither declaration
>>>is 'x' a pointer - its an array[4] of char.

>>
>>
>> You're the one who's wrong, here, ****nut --
>>

>I'm afraid the ****nut is quite correct.


Whoy, I resemble that remark!


--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      10-09-2005
On 7 Oct 2005 06:02:00 -0700, "werasm" <> wrote in
comp.lang.c:

>
> Davy wrote:
> > Hi all,
> >
> > I found
> > char x[4]={"my"};
> > can be compiled.

>
> Yes, but it is better to do this:
>
> char x[] = "my";
>
> This way just enough space for "my" is provided.
>
> > But
> > char x[4];
> > x={"my"};
> > can not be compiled.

>
> Because the type of x in...
>
> char x[4];
>
> ...is effectively...
>
> char* const x;


No, it is not. 'x' is not a pointer, not any kind of pointer to
anything.

> ...which means the value of the pointer "x" is constant - may not be
> modified, whereas doing this...


The statement above has no meaning, since there is no 'pointer "x"'.

> x={"my"}; //- actually x = "my"...
>
> ...attemps to modify the pointer x, which is why you get a compiler
> error.


Except that, once again, there is no 'pointer x'. 'x' is the name of
a region of storage that is large enough to store four chars. There
is no pointer involved.

> Suggestions as follow:
>
> char x[100] = { '\0' };
> std:strstream os( x, sizeof(x) );
> os << "Hooh, hah" << std::ends; //don't forget std::ends;
>
> or...
>
> std::string x("my");
>
> or...
>
> char x[4] = { '\0' };
> strcpy( x, "my" );
>
> Remember, the contents of the array which exists at the address
> location whereto x points, is modifiable. The address itself (or the


But 'x' does not point to anything.

> value of the ptr) is not.


There is no 'ptr' (sic).

> Regards,
>
> W


It is posts like this that help keep the confusion about arrays and
pointers worse than it has to be.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
werasm
Guest
Posts: n/a
 
      10-10-2005
> No, it is not. 'x' is not a pointer, not any kind of pointer to
> anything.


Hi Jack,

According to Bjarne Stroustrup:

"The name of an array can be used as a pointer to its initial element."

In my own words - "The name of an array is implicitly convertable to a
pointer pointing to the address location of its first element..."

While I do understand that x is in actual fact "the name of a region
that is large enough to store four chars" - or simply the name of an
array of characters of for elements, my point remains that, in contrast
to this case...

char* x = new char[4];

.... the type of x in case...

char x[4];

....is very similar (or effectively the same as) to...

char* const x = new char[4];

I did not say exactly the same (or equivalent to) - but reacts the same
in that one cannot do this:

char* const x( 0 );
char* y( new char [4] );
x = y;

Which would help (I hope) the initial poster to understand it a little
better (IMHO) - especially the reason for his compilation error.

>> It is posts like this that help keep the confusion about arrays and
>> pointers worse than it has to be.


I disagree with this. The fact that the type <char[4]> is implicitly
convertable ot <char*> is confusing. Using my explanation, I attempt to
clarify it to c++ users that find it confusing. If you feel that I've
failed to do so (clarify), suit yourself.

Thank you for your response anyway. Agreed - but my posting was misread
by you - I feel.

Regards,

W

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
'System.String[]' from its string representation 'String[] Array' =?Utf-8?B?UmFqZXNoIHNvbmk=?= ASP .Net 0 05-04-2006 04:29 PM
Is "String s = "abc";" equal to "String s = new String("abc");"? Bruce Sam Java 15 11-19-2004 06:03 PM
String[] files = {"a.doc, b.doc"}; VERSUS String[] files = new String[] {"a.doc, b.doc"}; Matt Java 3 09-17-2004 10:28 PM
String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 3 12-05-2003 04:20 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57