473,790 Members | 2,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String Problem?

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

Oct 7 '05
28 2131
Dale wrote:
Mark McIntyre <ma**********@s pamcop.net> wrote in
news:u8******** *************** *********@4ax.c om:
On 07 Oct 2005 17:53:50 GMT, in comp.lang.c , Dale <da***@gas.oran ge>
wrote:
"Davy" <zh*******@gmai l.com> wrote in news:1128661915 .425811.133260
@g44g2000cwa .googlegroups.c om:

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, fucknut -- '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_a rray>::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_p ointer>::value << '\n';
std::cout << std::boolalpha << is_pointer<char _pointer>::valu e << '\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
Oct 8 '05 #11
Dale wrote:
Mark McIntyre <ma**********@s pamcop.net> wrote in
news:u8******** *************** *********@4ax.c om:

On 07 Oct 2005 17:53:50 GMT, in comp.lang.c , Dale <da***@gas.oran ge>
wrote:
"Davy" <zh*******@gmai l.com> wrote in news:1128661915 .425811.133260
@g44g2000cwa .googlegroups.c om:

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, fucknut -- '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 fucknut 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.
Oct 8 '05 #12
Kai-Uwe Bux <jk********@gmx .net> writes:
Dale wrote:
Mark McIntyre <ma**********@s pamcop.net> wrote in
news:u8******** *************** *********@4ax.c om:
On 07 Oct 2005 17:53:50 GMT, in comp.lang.c , Dale <da***@gas.oran ge>
wrote:
"Davy" <zh*******@gmai l.com> wrote in news:1128661915 .425811.133260
@g44g2000cw a.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, fucknut -- '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_Keit h) ks***@mib.org <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.
Oct 8 '05 #13
Dale wrote:
Mark McIntyre <ma**********@s pamcop.net> wrote in
news:u8******** *************** *********@4ax.c om:
On 07 Oct 2005 17:53:50 GMT, in comp.lang.c , Dale <da***@gas.oran ge>
wrote:
"Davy" <zh*******@gmai l.com> wrote in news:1128661915 .425811.133260
@g44g2000cwa .googlegroups.c om:

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, fucknut
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 fucking 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.
Oct 8 '05 #14
Dale wrote:
Mark McIntyre <ma**********@s pamcop.net> wrote in
news:u8******** *************** *********@4ax.c om:
On 07 Oct 2005 17:53:50 GMT, in comp.lang.c , Dale <da***@gas.oran ge>
wrote:
"Davy" <zh*******@gmai l.com> wrote in news:1128661915 .425811.133260
@g44g2000cwa .googlegroups.c om:

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, fucknut -- '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 fucking 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.
Oct 8 '05 #15
Dale wrote:

You're the one who's wrong, here, fucknut -- '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
Oct 8 '05 #16
On 08 Oct 2005 19:26:59 GMT, in comp.lang.c , Dale <da***@gas.oran ge>
wrote:
Mark McIntyre <ma**********@s pamcop.net> wrote in
news:u8******* *************** **********@4ax. com:
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, fucknut


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 =----
Oct 8 '05 #17
On Sat, 08 Oct 2005 22:32:20 +0200, in comp.lang.c , Skarmander
<in*****@dontma ilme.com> wrote:
Dale wrote:
Mark McIntyre <ma**********@s pamcop.net> 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, fucknut --

I'm afraid the fucknut 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 =----
Oct 8 '05 #18
On 7 Oct 2005 06:02:00 -0700, "werasm" <w_*****@telkom sa.net> 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::ostrstream 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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Oct 9 '05 #19
> 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

Oct 10 '05 #20

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

Similar topics

7
10629
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using namespace std; : : vector<string>* getTyphoon()
17
14363
by: Olivier Bellemare | last post by:
I've tried to make a function that returns the middle of a string. For example: strmid("this is a text",6,4); would return "is a". Here is my code: char *strmid(char *texte, int depart, int longueur) { char *resultat = " "; char *temporaire = " "; int nbr;
51
8298
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct code? many thx!
18
7198
by: Steve Litvack | last post by:
Hello, I have built an XMLDocument object instance and I get the following string when I examine the InnerXml property: <?xml version=\"1.0\"?><ROOT><UserData UserID=\"2282\"><Tag1 QID=\"55111\"><Tag2 AID=\"5511101\"></Tag2></Tag1><Tag1 QID=\"55112\"><Tag2 AID=\"5511217\"></Tag2></Tag1><Tag1 QID=\"5512282\"><Tag2 AID=\"551228206\"></Tag2></Tag1><Tag1 QID=\"55114\"><Tag2 AID=\"5511406\"></Tag2></Tag1><Tag1 QID=\"55115\"><Tag2
32
14902
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
12
9646
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" & Chr(34) & PopUpWindowTitle & Chr(34) & ", " & Chr(34) & CurrentEventDetails & ")" strTemp += "<BR><A HREF='#' onClick='" & PopupLink & "'>" & EventName & "</A><BR>" The problem I have is that when the string variables or contain a string with an...
4
5281
by: MooMaster | last post by:
After some google searching on the forum I couldn't find any topics that seemed to relate exactly to my problem, so hopefully someone can help me out... I'm running python 2.4.1 on a local Win2K system and doing some work-related development on an AIX box, and I'm running into a problem using cx_Oracle to add some information to an Oracle 9.2 table. I have a table_title defined as a VARCHAR2 data type, and when I'm trying to retrieve...
6
2215
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time and resource consuming Try... Catch, which in iterative processing is totally unacceptable. -tom
5
3537
by: ThatVBGuy | last post by:
Hello All, I could really use some help with this problem its driving me nuts. I have a small vb app, the goal of the app is to read an html doc into a variable then go through that variable and find and replace some tags. I have 3 functions. 1 to open the doc, the 2nd to find and replace the tags the 3rd to save the info. the code is pasted below : Public Function ReadFileContents(FileFullPath As String) As _ String On Error GoTo...
1
8378
Atli
by: Atli | last post by:
The following small HowTo is a compilation of an original problem in getting some cookie-values through different methods of string-handling. The original Problem was posted as follows: As you can see, there could have been a problem with the split-method. The following short article handles ways around this possible problem, that we couldn't reproduce, but someone may possibly encounter it too sometimes. If nothing else, the shown...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10419
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9987
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9023
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6770
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3709
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2910
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.