Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #19257

Re: What's wrong with this simple code?

From "BartC" <bc@freeuk.com>
Newsgroups comp.lang.c
Subject Re: What's wrong with this simple code?
Date 2012-04-02 22:07 +0100
Organization A noiseless patient Spider
Message-ID <jld4fc$557$1@dont-email.me> (permalink)
References <48d7ec09-3c0d-4e4a-9c95-3479417fbf34@x5g2000pbl.googlegroups.com> <4F70ACDA.9040902@verizon.net> <7325db7d-ab77-4838-b7f4-0c884bc52e04@oq7g2000pbb.googlegroups.com>

Show all headers | View raw



"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 

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

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

csiph-web