Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Char []

Reply
Thread Tools

Char []

 
 
Rick
Guest
Posts: n/a
 
      10-14-2003
Hi,

Before copying something into a character array, do we need to
initialize it with something? For example:

char a[10];

strcpy(a, "hey");

Can we do:

char a[10] = {0};

strcpy(a, "hey");


Thanks

Rick

 
Reply With Quote
 
 
 
 
Vijay Kumar R Zanvar
Guest
Posts: n/a
 
      10-14-2003


"Rick" <rrquick@nospam-com> wrote in message
news:3f8bade2$(E-Mail Removed)...
> Hi,
>
> Before copying something into a character array, do we need to
> initialize it with something? For example:
>
> char a[10];
>
> strcpy(a, "hey");
>
> Can we do:
>
> char a[10] = {0};
>
> strcpy(a, "hey");
>

You can write:

char a[] = "hey";


>
> Thanks
>
> Rick
>


 
Reply With Quote
 
 
 
 
Rick
Guest
Posts: n/a
 
      10-14-2003
> You can write:
>
> char a[] = "hey";


I need to copy a string into the array at a later stage.

Rick

 
Reply With Quote
 
Alex
Guest
Posts: n/a
 
      10-14-2003
"Rick" <rrquick@nospam-com> wrote in message
news:3f8bade2$(E-Mail Removed)...
> Before copying something into a character array, do we need to
> initialize it with something?


No. It serves no purpose, but you can if you want to.

Try writing a function my_strcpy() that behaves like strcpy(). Does this
function care what the contents of the array are beforehand?

Alex


 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      10-14-2003
Rick <rrquick@nospam-com> wrote:

> Before copying something into a character array, do we need to
> initialize it with something? For example:


No.

> char a[10];
>
> strcpy(a, "hey");
>
> Can we do:
>
> char a[10] = {0};
>
> strcpy(a, "hey");


You can, but you don't need to. It serves no purpose whatsoever, except
possibly to keep braindead compilers from issuing warnings.

Richard
 
Reply With Quote
 
Andreas Kahari
Guest
Posts: n/a
 
      10-14-2003
In article <3f8bb9f4$(E-Mail Removed)>, Rick wrote:
>> You can write:
>>
>> char a[] = "hey";

>
> I need to copy a string into the array at a later stage.


No, you don't need to initialize the string. You only need to
make sure that the string i long enough to hold the data that
you're copying to it.

If you're copying the string character by character yourself,
make sure to place a nul character (string terminator, '\0')
after the last "visible" character in the string, to terminate
the string. Most library functions handles the string
termination themselves (read about each function you use).

The terminator counts as one charcater, so there must be room
for it as well.


--
Andreas Kähäri
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-14-2003
Rick <rrquick@nospam-com> wrote:

>Before copying something into a character array, do we need to
>initialize it with something? For example:
>
>char a[10];
>strcpy(a, "hey");
>
>Can we do:
>
>char a[10] = {0};
>strcpy(a, "hey");


You can, but you don't have to.
Just for completeness:

char a[10] = "hey";

is fine, too.

Regards
--
Irrwahn
((E-Mail Removed))
 
Reply With Quote
 
Rick
Guest
Posts: n/a
 
      10-14-2003
Does the following hold any meaning on its own:

char a[10] = {0};

I know this works for structs.. but I'm not sure about character arrays.
Thanks

Rick


 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-14-2003
Rick <rrquick@nospam-com> wrote:

>Does the following hold any meaning on its own:
>
>char a[10] = {0};


Initialize all elements of array a to 0.

Regards
--
Irrwahn
((E-Mail Removed))
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      10-14-2003
On Tue, 14 Oct 2003 19:05:42 +1000, in comp.lang.c , Rick
<rrquick@nospam-com> wrote:

>> You can write:
>>
>> char a[] = "hey";

>
>I need to copy a string into the array at a later stage.


in that case, make it big enogh when you declare it
char a[BIG_ENOUGH] = "pointless initialiser";

The initialiser serves no purpose except to cause unexpected errors if
you use memcpy or strcat carelessly. This may of course be a useful
purpose. I sometimes initialise strings with '\0' to allow better
error checking, or so that you don't need to consider the first copy
into it a special case. .

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char *fred; char * fred; char *fred; any difference? Ben Pfaff C Programming 5 01-17-2004 07:37 PM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments