String Problem?

D

Davy

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
 
S

sat

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

Kid Hugh

You can use the following:
strcpy(x,"my");
instead of
x={"my"};

x is an array pointer not only a variable.
 
W

werasm

Davy said:
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::eek: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
 
D

Dale

Davy said:
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.
 
M

Mark McIntyre

Davy said:
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.
 
D

Dale

Davy said:
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.

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 fucking compile because '*x'
would be considered invalid indirection.

Dumbass.
 
J

John Harrison

Dale said:
@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, fucknut -- '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 fucking 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.

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
 
K

Kai-Uwe Bux

Dale said:
@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, 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_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
 
K

Keith Thompson

Dale said:
Mark McIntyre said:
@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, 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.

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

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

Skarmander

Dale said:
@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, 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>

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

S.
 
K

Keith Thompson

Kai-Uwe Bux said:
Dale said:
Mark McIntyre said:
On 07 Oct 2005 17:53:50 GMT, in comp.lang.c , Dale <[email protected]>
wrote:
@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, 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.
 
F

Flash Gordon

Dale said:
@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, 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.

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

Martin Ambuhl

Dale said:
Mark McIntyre said:
@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, 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

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

Default User

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
 
M

Mark McIntyre

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.

Physician, heal thyself.
 
D

Dale

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.

Too bad you can't prove me wrong, huh, Mr. Thinskin.
Arrays are not pointers.

I've already shown that an identifier declared as an array of characters is
a character type pointer. You have failed to demonstrate otherwise.
 
M

Mark McIntyre

I've already shown that an identifier declared as an array of characters is
a character type pointer.

What you've demonstrated is
a) that when passed to a function, an array decays into a pointer to
its first element
b) you're an idiot
You have failed to demonstrate otherwise.

Actually, I've failed to demonstrate that you can learn from your
mistakes. For that I apologise.

*plonk*
 

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
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top