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


Groups > comp.lang.c > #19060

Re: What's wrong with this simple code?

From jt@toerring.de (Jens Thoms Toerring)
Newsgroups comp.lang.c
Subject Re: What's wrong with this simple code?
Date 2012-03-26 16:50 +0000
Organization Freie Universitaet Berlin
Message-ID <9tbl2gFi2uU1@mid.uni-berlin.de> (permalink)
References <48d7ec09-3c0d-4e4a-9c95-3479417fbf34@x5g2000pbl.googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.c | Previous | Next — Previous in thread | Next 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