473,513 Members | 2,493 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Char []

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

Nov 13 '05 #1
17 2362


"Rick" <rrquick@nospam-com> wrote in message
news:3f********@clarion.carno.net.au...
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


Nov 13 '05 #2
> You can write:

char a[] = "hey";


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

Rick

Nov 13 '05 #3
"Rick" <rrquick@nospam-com> wrote in message
news:3f********@clarion.carno.net.au...
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
Nov 13 '05 #4
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
Nov 13 '05 #5
In article <3f********@clarion.carno.net.au>, 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
Nov 13 '05 #6
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
(ir*******@freenet.de)
Nov 13 '05 #7
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
Nov 13 '05 #8
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
(ir*******@freenet.de)
Nov 13 '05 #9
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 =---
Nov 13 '05 #10
On Tue, 14 Oct 2003 14:20:20 +0200, in comp.lang.c , Irrwahn
Grausewitz <ir*******@freenet.de> wrote:
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.


strictly, I believe it initialises the first element to the value
inside the braces, and then the rest to zero or a null pointer as
appropriate for the data type. Which adds up to the same thing for
most data types.

--
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 =---
Nov 13 '05 #11


Rick wrote:
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");


Whether you initialize or not initialize will depend on your
intended use of the char array.
If you are function strcpy, the destination char array does
not need to be initialized. If you are going to use the char
array as the destination for the strcat family of functions,
you wll need to initialize it or somehow make it a string
before you use it.

Ex:
An empty string:
char a[16] = "";
or
char a[16] = {'\0'};

strcat(a,"Hello World");
char array a would contain the string "Hello World".

You can initialize with a string and append.
char a[16] = "Hello ";
strcat(a,"World");
char array a would contain the string "Hello World".

--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #12
Mark McIntyre <ma**********@spamcop.net> wrote:
On Tue, 14 Oct 2003 14:20:20 +0200, in comp.lang.c , Irrwahn
Grausewitz <ir*******@freenet.de> wrote:
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.


strictly, I believe it initialises the first element to the value
inside the braces, and then the rest to zero [...].


That's more accurate, right. However, given the example above, the
result is that all elements are initialized to zero, anyway.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #13
Rick <rrquick@nospam-com> wrote in message news:<3f********@clarion.carno.net.au>...
Hi,

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


Generally, no. If you're using strcpy() or strcat() to write to a
string, it doesn't matter what was there before. Depending on the
circumstances, I might initialize the contents of an array if there's
a chance it will be read by something before it's been written to.
Nov 13 '05 #14
John Bode wrote:
Rick <rrquick@nospam-com> wrote in message news:<3f********@clarion.carno.net.au>...
Before copying something into a character array, do we need to
initialize it with something? For example:


Generally, no. If you're using strcpy() or strcat() to write to a
string, it doesn't matter what was there before.


strcat() is not a good example; it relies on the destination buffer
containing a string.

Jeremy.
Nov 13 '05 #15
On Tue, 14 Oct 2003 17:48:55 +0200, in comp.lang.c , Irrwahn
Grausewitz <ir*******@freenet.de> wrote:
Mark McIntyre <ma**********@spamcop.net> wrote:
On Tue, 14 Oct 2003 14:20:20 +0200, in comp.lang.c , Irrwahn
Grausewitz <ir*******@freenet.de> wrote:
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.


strictly, I believe it initialises the first element to the value
inside the braces, and then the rest to zero [...].


That's more accurate, right. However, given the example above, the
result is that all elements are initialized to zero, anyway.


Absolutely., I wanted to avoid a potential misunderstanding.

--
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 =---
Nov 13 '05 #16
Jeremy Yallop <je****@jdyallop.freeserve.co.uk> wrote in message news:<sl*******************@ziba.cl.cam.ac.uk>...
John Bode wrote:
Rick <rrquick@nospam-com> wrote in message news:<3f********@clarion.carno.net.au>...
Before copying something into a character array, do we need to
initialize it with something? For example:


Generally, no. If you're using strcpy() or strcat() to write to a
string, it doesn't matter what was there before.


strcat() is not a good example; it relies on the destination buffer
containing a string.

Jeremy.


You're absolutely right. I was a bit fried when I wrote that.
Nov 13 '05 #17
you might want to initialize it when using functions like strncpy or
make sure that after you copy n bytes to a string that "string"[n] is 0.

John Bode wrote:
Rick <rrquick@nospam-com> wrote in message news:<3f********@clarion.carno.net.au>...
Hi,

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

Generally, no. If you're using strcpy() or strcat() to write to a
string, it doesn't matter what was there before. Depending on the
circumstances, I might initialize the contents of an array if there's
a chance it will be read by something before it's been written to.


Nov 13 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3518
by: Yang Song | last post by:
HI, I am a little confused about char * and char. How would I be able to return a char* created in a function? Is new() the only way? How would I be able to return a point and a value at the same...
5
9717
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an...
5
2512
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
2
3391
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
5
3940
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
12
10054
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
18
4025
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
4
3189
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
16
6762
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
29
9925
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5089
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.