Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

String Problem?

 
 
Davy
Guest
Posts: n/a
 
      10-07-2005
Hi all,

I found
char x[4]={"my"};
can be compiled.

But
char x[4];
x={"my"};
can not be compiled.

Why?
Any suggestions will be appreciated!

Best regards,
Davy

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      10-07-2005
In article <(E-Mail Removed) .com>,
Davy <(E-Mail Removed)> wrote:
>I found
> char x[4]={"my"};
>can be compiled.


>But
> char x[4];
> x={"my"};
>can not be compiled.


>Why?


Because that's how it is.

Initializers on a variable declaration have syntaxes available
that are not available in other places.
--
Camera manufacturers have temporarily delayed introduction of
sub-millibarn resolution bio-hyperdimensional plasmatic space polyimaging,
but indications are that is still just around the corner.
 
Reply With Quote
 
 
 
 
sat
Guest
Posts: n/a
 
      10-07-2005
Ok davy, to understand this,
you need some background on the way C / C++ deals with array names and array
pointers.

char* p1 = "hello";
char p2[] = hello;

now.. p1 and p2 can be used in most places interchangeable.. like
1. p1[0];
2. *(p2+2)
etc etc..
but there are few exceptioins for this.
1. p2++
2. p2 - =10;
3. p2 = p1
4. char* ch = "world" ;
p2 = ch;
ARE NOT VALID

the thumb rule is "you cant do any modication operations to the address
pointed by the array name". You will get an LValue required error.




"Davy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi all,
>
> I found
> char x[4]={"my"};
> can be compiled.
>
> But
> char x[4];
> x={"my"};
> can not be compiled.
>
> Why?
> Any suggestions will be appreciated!
>
> Best regards,
> Davy
>



 
Reply With Quote
 
Kid Hugh
Guest
Posts: n/a
 
      10-07-2005
You can use the following:
strcpy(x,"my");
instead of
x={"my"};

x is an array pointer not only a variable.

"Davy" <(E-Mail Removed)> ????
news:(E-Mail Removed) oups.com...
> Hi all,
>
> I found
> char x[4]={"my"};
> can be compiled.
>
> But
> char x[4];
> x={"my"};
> can not be compiled.
>
> Why?
> Any suggestions will be appreciated!
>
> Best regards,
> Davy
>



 
Reply With Quote
 
werasm
Guest
Posts: n/a
 
      10-07-2005

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;

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

x={"my"}; //- actually x = "my"...

....attemps to modify the pointer x, which is why you get a compiler
error.

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
value of the ptr) is not.

Regards,

W


>
> Why?
> Any suggestions will be appreciated!
>
> Best regards,
> Davy


 
Reply With Quote
 
Dale
Guest
Posts: n/a
 
      10-07-2005
"Davy" <(E-Mail Removed)> wrote in news:1128661915.425811.133260
@g44g2000cwa.googlegroups.com:
>
> Hi all,
>
> I found
> char x[4]={"my"};
> can be compiled.
>
> But
> char x[4];
> x={"my"};
> can not be compiled.
>
> Why?


Remember. sweetums, 'x' is a pointer when you declare it that way. If that
assignment actually worked, it would be changing the pointer itself, not
the data that the pointer points to.

Be glad it didn't compile, because the resulting program would have either
crashed or produced bizarre results, and then you'd be sitting there all
day trying to figure out why.
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      10-07-2005
On 07 Oct 2005 17:53:50 GMT, in comp.lang.c , Dale <(E-Mail Removed)>
wrote:
>"Davy" <(E-Mail Removed)> 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.

>If that assignment actually worked, it would be changing the pointer itself, not
>the data that the pointer points to.


Which would be perfectly legal for a pointer. However for an array,
you simply can't do that.
--
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
 
Dale
Guest
Posts: n/a
 
      10-08-2005
Mark McIntyre <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> On 07 Oct 2005 17:53:50 GMT, in comp.lang.c , Dale <(E-Mail Removed)>
> wrote:
>>"Davy" <(E-Mail Removed)> 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.

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

Dumbass.
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      10-08-2005
Dale wrote:
> Mark McIntyre <(E-Mail Removed)> wrote in
> news:(E-Mail Removed):
>
>
>>On 07 Oct 2005 17:53:50 GMT, in comp.lang.c , Dale <(E-Mail Removed)>
>>wrote:
>>
>>>"Davy" <(E-Mail Removed)> 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


No x is an array

> 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.


That is just confused. Arrays are stored as 'a chunk of RAM', pointers
don't enter the picture.

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


You clearly don't know the rule in C and C++ which allows an array to
convert to a pointer in many cirumstances. x can convert to a pointer,
and that is what it is doing in both your examples above, but that does
not mean that x is a pointer.

Try this program out

int main()
{
char x[99];
printf("%u\n", sizeof x);
}

It x really was a pointer then that would print 4 (the size of a
pointer) but it prints 99 (the sizeof the array). That is because sizeof
is on of the exception to the rule that an array can be converted to a
pointer. There are other exceptions as well but they are rare, so I can
see how you might have missed this.

>
> Dumbass.


If you are going to post to a public forum, you have accept the
possiblity that you might be wrong, and that you might be corrected. It
stops everyone else thinking you are a wanker if you can accept those
occaisions with good grace.

john
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-08-2005
Dale wrote:

> Mark McIntyre <(E-Mail Removed)> wrote in
> news:(E-Mail Removed):
>
>> On 07 Oct 2005 17:53:50 GMT, in comp.lang.c , Dale <(E-Mail Removed)>
>> wrote:
>>>"Davy" <(E-Mail Removed)> 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'.
Consider:

#include <iostream>
#include <iomanip>
#include <typeinfo>

template < typename T >
struct is_array {

static const bool value = false;

};

template < typename T, unsigned long N >
struct is_array< T[N] > {

static const bool value = true;

};

template < typename T >
struct is_pointer {

static const bool value = false;

};

template < typename T >
struct is_pointer< T* > {

static const bool value = true;

};


int main ( void ) {
char x [4];

typedef char char_array [4];
char_array y;

typedef char* char_pointer;
char_pointer z;

std::cout << std::boolalpha << ( typeid(x) == typeid(y) ) << '\n';
std::cout << std::boolalpha << is_array<char_array>::value << '\n';
std::cout << std::boolalpha << is_pointer<char_array>::value << '\n';
std::cout << '\n';
std::cout << std::boolalpha << ( typeid(x) == typeid(z) ) << '\n';
std::cout << std::boolalpha << is_array<char_pointer>::value << '\n';
std::cout << std::boolalpha << is_pointer<char_pointer>::value << '\n';
}

This will print:
true
true
false

false
false
true


Maybe, you are correct within the context of the C programming language,
though.



Best

Kai-Uwe Bux
 
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