Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #19056 > unrolled thread
| Started by | fajar96te <fajar.suryawan@gmail.com> |
|---|---|
| First post | 2012-03-26 09:28 -0700 |
| Last post | 2012-04-02 22:07 +0100 |
| Articles | 10 — 8 participants |
Back to article view | Back to comp.lang.c
What's wrong with this simple code? fajar96te <fajar.suryawan@gmail.com> - 2012-03-26 09:28 -0700
Re: What's wrong with this simple code? blp@cs.stanford.edu (Ben Pfaff) - 2012-03-26 09:45 -0700
Re: What's wrong with this simple code? Jens Gustedt <jens.gustedt@loria.fr> - 2012-03-26 18:45 +0200
Re: What's wrong with this simple code? "BartC" <bc@freeuk.com> - 2012-03-26 17:45 +0100
Re: What's wrong with this simple code? jt@toerring.de (Jens Thoms Toerring) - 2012-03-26 16:50 +0000
Re: What's wrong with this simple code? James Kuyper <jameskuyper@verizon.net> - 2012-03-26 13:52 -0400
Re: What's wrong with this simple code? nick_keighley_nospam@hotmail.com - 2012-03-29 00:16 -0700
Re: What's wrong with this simple code? fajar96te <fajar.suryawan@gmail.com> - 2012-03-28 17:23 -0700
Re: What's wrong with this simple code? Sjouke Burry <s@b> - 2012-04-02 20:39 +0000
Re: What's wrong with this simple code? "BartC" <bc@freeuk.com> - 2012-04-02 22:07 +0100
| From | fajar96te <fajar.suryawan@gmail.com> |
|---|---|
| Date | 2012-03-26 09:28 -0700 |
| Subject | What's wrong with this simple code? |
| Message-ID | <48d7ec09-3c0d-4e4a-9c95-3479417fbf34@x5g2000pbl.googlegroups.com> |
When I tried to compile this:
// ==============
#include <stdio.h>
void main()
{
char k[4] ;
k = "abc" ;
}
// ==============
I got:
error: incompatible types when assigning to type 'char[4]' from type
'char *'
I'm using gcc on MinGW and Windows 7.
What's going on?
Thanks
[toc] | [next] | [standalone]
| From | blp@cs.stanford.edu (Ben Pfaff) |
|---|---|
| Date | 2012-03-26 09:45 -0700 |
| Message-ID | <87haxb71lf.fsf@blp.benpfaff.org> |
| In reply to | #19056 |
fajar96te <fajar.suryawan@gmail.com> writes:
> When I tried to compile this:
>
> // ==============
> #include <stdio.h>
> void main()
> {
> char k[4] ;
> k = "abc" ;
> }
> // ==============
>
> I got:
> error: incompatible types when assigning to type 'char[4]' from type
> 'char *'
You can't assign to an array.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
[toc] | [prev] | [next] | [standalone]
| From | Jens Gustedt <jens.gustedt@loria.fr> |
|---|---|
| Date | 2012-03-26 18:45 +0200 |
| Message-ID | <4F709D14.6020003@loria.fr> |
| In reply to | #19056 |
Am 03/26/2012 06:28 PM, schrieb fajar96te:
> When I tried to compile this:
>
> // ==============
> #include <stdio.h>
> void main()
> {
> char k[4] ;
> k = "abc" ;
> }
Please consider reading the FAQ, first:
http://c-faq.com/aryptr/index.html
Jens
[toc] | [prev] | [next] | [standalone]
| From | "BartC" <bc@freeuk.com> |
|---|---|
| Date | 2012-03-26 17:45 +0100 |
| Message-ID | <jkq6gj$b2v$1@dont-email.me> |
| In reply to | #19056 |
"fajar96te" <fajar.suryawan@gmail.com> wrote in message
news:48d7ec09-3c0d-4e4a-9c95-3479417fbf34@x5g2000pbl.googlegroups.com...
> When I tried to compile this:
>
> // ==============
> #include <stdio.h>
> void main()
> {
> char k[4] ;
> k = "abc" ;
> }
> // ==============
>
> I got:
> error: incompatible types when assigning to type 'char[4]' from type
> 'char *'
Try:
strcpy(k,"abc");
You might need to include string.h.
--
Bartc
[toc] | [prev] | [next] | [standalone]
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Date | 2012-03-26 16:50 +0000 |
| Message-ID | <9tbl2gFi2uU1@mid.uni-berlin.de> |
| In reply to | #19056 |
fajar96te <fajar.suryawan@gmail.com> wrote:
> When I tried to compile this:
> // ==============
> #include <stdio.h>
> void main()
make that
int main( void )
main() is supposed to return an int and either takes no argumemts
or two (and not an unspecified number, which is the meaning of
not specifying any arguments.
> {
> char k[4] ;
> k = "abc" ;
'k' is an array of chars and you can't assign to an array as
a whole (that holds for all kinds of arrays, not just arrays
of chars). Moreover, what you have on the right hand of the
equal sign is not an array of chars but a pointer to a string
literal (i.e a pointer to a const array of chars), so you have
two things going wrong here - on the one hand you try to assign
to an array and then you have different tyoes on both sides of
teh assignment.
What you may have seen is somthing like
char k[4] = "abc";
The important difference is that this isn't an assignment but
an initialization of an array - and this can only be done while
the array is defined. And that you can use a string literal here
is some special case in C to make it easier to initialize
strigs, without this special case you'd have to write
char k[4] = { 'a', 'b', c', '\0' };
as you have in any other type of array initialization.
Compare this to an int array: an initialization like
int x[3] = { 1, 42, -17 };
int y[3];
is fine, but you can't do neither
x = { 1, 2, 5 };
nor
y = x;
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2012-03-26 13:52 -0400 |
| Message-ID | <4F70ACDA.9040902@verizon.net> |
| In reply to | #19056 |
On 03/26/2012 12:28 PM, fajar96te wrote:
> When I tried to compile this:
>
> // ==============
> #include <stdio.h>
> void main()
> {
> char k[4] ;
> k = "abc" ;
> }
> // ==============
>
> I got:
> error: incompatible types when assigning to type 'char[4]' from type
> 'char *'
>
> I'm using gcc on MinGW and Windows 7.
>
>
> What's going on?
Other people have answered your question fairly completely, but no one
seems to have mentioned one of the two main alternative ways of fixing
your code:
const char *k;
k = "abc";
The string literal "abc" calls for allocation of an unnamed array of
char with four elements; the string literal itself is automatically
converted to a char* pointer to the first element of that array. Despite
the fact that it's a char*, rather than a const char*, it's undefined
behavior if your program tries to write to that array. This is because
'const' wasn't added to the language until long to Therefore, it's safer
to store the pointer in a const char*.
If you need to modify the array after creating it, don't use this
method. Use
char k[] = "abc";
instead.
[toc] | [prev] | [next] | [standalone]
| From | nick_keighley_nospam@hotmail.com |
|---|---|
| Date | 2012-03-29 00:16 -0700 |
| Message-ID | <32414541.2329.1333005375183.JavaMail.geo-discussion-forums@vbhv6> |
| In reply to | #19061 |
On Thursday, March 29, 2012 1:23:12 AM UTC+1, fajar96te wrote: > This left me wonder... is there any design-reason behind this peculiar > grammar of C? it's not a grammer thing its a sematics (meaning) thing. I must have been programming in C for too long; it doesn't look in the slightest bit peculiar to me! > i.e., that one cannot assign to an array? Will it hurt > if one can directly assign an array as easily as k = "abc"; as one > would an integer? It's probably as simple as Dennis Richie didn't see a need for it. C statements usually expand to very little machine code. Array assignment would expand to quite a lot. Though this arguemtn is undermined by the observation that struct assignment, which is allowed, also expands to quite a lot of code. I suspect the semantics of array argument passing would become a bit messy. You don't really pass an array argument to a function, ot "decays" into a pointer. I suspect array assignment would have quite a ripple affect through other parts of the language.
[toc] | [prev] | [next] | [standalone]
| From | fajar96te <fajar.suryawan@gmail.com> |
|---|---|
| Date | 2012-03-28 17:23 -0700 |
| Message-ID | <7325db7d-ab77-4838-b7f4-0c884bc52e04@oq7g2000pbb.googlegroups.com> |
| In reply to | #19061 |
Thank you guys! This left me wonder... is there any design-reason behind this peculiar grammar of C? i.e., that one cannot assign to an array? Will it hurt if one can directly assign an array as easily as k = "abc"; as one would an integer?
[toc] | [prev] | [next] | [standalone]
| From | Sjouke Burry <s@b> |
|---|---|
| Date | 2012-04-02 20:39 +0000 |
| Message-ID | <XnsA029E662766B4sjoukeburrysoesterbe@213.75.12.10> |
| In reply to | #19237 |
fajar96te <fajar.suryawan@gmail.com> wrote in news:7325db7d-ab77-4838-b7f4- 0c884bc52e04@oq7g2000pbb.googlegroups.com: > Thank you guys! > > This left me wonder... is there any design-reason behind this peculiar > grammar of C? i.e., that one cannot assign to an array? Will it hurt > if one can directly assign an array as easily as k = "abc"; as one > would an integer? > > Start programming in Fortran.
[toc] | [prev] | [next] | [standalone]
| From | "BartC" <bc@freeuk.com> |
|---|---|
| Date | 2012-04-02 22:07 +0100 |
| Message-ID | <jld4fc$557$1@dont-email.me> |
| In reply to | #19237 |
"fajar96te" <fajar.suryawan@gmail.com> wrote in message news:7325db7d-ab77-4838-b7f4-0c884bc52e04@oq7g2000pbb.googlegroups.com... > Thank you guys! > > This left me wonder... is there any design-reason behind this peculiar > grammar of C? i.e., that one cannot assign to an array? Will it hurt > if one can directly assign an array as easily as k = "abc"; as one > would an integer? You're probably confusing two concepts: arrays and strings. Fixed-bound arrays *could* have been treated as value-types like numeric types, pointers and structs (so they are manipulated by value, passed to functions by value, and so on). But a decision was made in C not to allow value semantics for arrays; instead you can only manipulate array pointers. It could have been possible to have both value-arrays and array-pointers, but with extra syntax to distinguish the two uses. However the loss isn't that great, because whole-array operations are rare, passing by value is inefficient, while working with arrays of different lengths is tricky because the length becomes part of the type. With strings, things get more complicated because as well as a fixed-bound char array, you also have this variable length of string in each one. For example, how do you implement this: char a[10000],b[10000]; a[0]='X'; /* a contains "X" a[1]=0; b=a; Do you copy 2 characters, or 10,000? It's not clear whether these char arrays contain strings, or contain arbitrary data. -- Bartc
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web