469,283 Members | 2,303 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,283 developers. It's quick & easy.

Initialize char array in one line

Hi
How do i reinitialize a char array in one line if i want to use the same array variable every time.
Ex-

char name[1024];

-Ashok
Aug 19 '08 #1
9 6916
JosAH
11,448 Expert 8TB
You can't 're-initialize' anything, you can just assign another value to the members
of the array. Why do you want to do that on one line? Homework?

kind regards,

Jos
Aug 19 '08 #2
i'm not sure..
i will ask my lecturer tomorrow.
i hope i can help you..
Aug 19 '08 #3
This is not just a home wark.
If i want to use the same array , then i have to re-initialize it so it will be very easy if i can do it in one line.

any suggestion..
-ASK
Aug 23 '08 #4
leejwen
50
This is not just a home wark.
If i want to use the same array , then i have to re-initialize it so it will be very easy if i can do it in one line.

any suggestion..
-ASK
memset(name, 0, 1024);
Aug 23 '08 #5
Banfa
9,064 Expert Mod 8TB
You could assign new data to the array using strcpy or memcpy
Aug 23 '08 #6
Can memset be use for C++.

Regards,
Ask
Aug 23 '08 #7
Banfa
9,064 Expert Mod 8TB
Yes, of course if you are using C++ you should consider using a vector instead of an array with all its associated member.
Aug 23 '08 #8
boxfish
469 Expert 256MB
I've never seen memset before, so it seems wierd to me. Isn't strcpy a better choice somehow? It was designed for this, right?
Aug 23 '08 #9
JosAH
11,448 Expert 8TB
I've never seen memset before, so it seems wierd to me. Isn't strcpy a better choice somehow? It was designed for this, right?
The strcpy() function needs a \0 terminated sequence of chars; memset() simply
sets every element to the same value. Also memcpy() can be of use; it depends.

kind regards,

Jos
Aug 23 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by steveneng | last post: by
74 posts views Thread by Peter | last post: by
9 posts views Thread by Niels Dekker - no reply address | last post: by
6 posts views Thread by Ramprasad A Padmanabhan | last post: by
10 posts views Thread by Yang Lee | last post: by
15 posts views Thread by thinktwice | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.