Char []

R

Rick

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
 
V

Vijay Kumar R Zanvar

Rick said:
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";
 
A

Alex

Rick said:
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
 
R

Richard Bos

Rick said:
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
 
A

Andreas Kahari

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.
 
I

Irrwahn Grausewitz

Rick said:
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
 
R

Rick

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
 
M

Mark McIntyre

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. .
 
M

Mark McIntyre

Rick said:
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.
 
A

Al Bowers

Rick said:
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".
 
I

Irrwahn Grausewitz

Mark McIntyre said:
Rick said:
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
 
J

John Bode

Rick said:
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.
 
R

Ron Croonenberg

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.
 
J

Jeremy Yallop

John said:
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.
 
M

Mark McIntyre

Mark McIntyre said:
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.
 
J

John Bode

Jeremy Yallop said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top