473,703 Members | 2,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initialize char array in one line

30 New Member
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 7274
JosAH
11,448 Recognized Expert MVP
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
huzen88
3 New Member
i'm not sure..
i will ask my lecturer tomorrow.
i hope i can help you..
Aug 19 '08 #3
ashokd001
30 New Member
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 New Member
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,065 Recognized Expert Moderator Expert
You could assign new data to the array using strcpy or memcpy
Aug 23 '08 #6
ashokd001
30 New Member
Can memset be use for C++.

Regards,
Ask
Aug 23 '08 #7
Banfa
9,065 Recognized Expert Moderator Expert
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 Recognized Expert Contributor
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 Recognized Expert MVP
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

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

Similar topics

6
2578
by: steveneng | last post by:
C++ Primer Plus Programming Exercises 4th Ed - Prate Help I'm trying to refresh myself and I'm stuck on this problem (not homework/school related but for personal advancement). 6: Do programming exercise 3, but, instead of declaring an array of three CandyBar structures, use new to allocate the array dynamically. That's the question in case you guys don't ahve the book. I have Exercises 3 done and I can properly use structures. But...
74
43160
by: Peter | last post by:
Hi, So many times, I have seen compile warning: "you used a char* without initilize it", probably on the code like this: ------------ char* ptr; func(..., ptr); ----------
9
27070
by: Niels Dekker - no reply address | last post by:
Are all the following initializations semantically equivalent? wchar_t a = {L'\0'}; wchar_t b = {'\0'}; wchar_t c = {0}; wchar_t d = {}; If so, why don't we all use an empty initializer list, {}, when zero-initializing a C-style string? I was wondering, because the book C++ Coding Standards (Sutter & Alexandrescu) says at item 19, "Always
6
54976
by: Ramprasad A Padmanabhan | last post by:
I have a simple structure defined like this struct userid { char uid; int insize; int outsize; }; typedef struct userid user;
10
43460
by: Yang Lee | last post by:
hi is this correct char teststr="\0"; or is there another method to initialise; thanks lee
1
4715
by: DC | last post by:
Hi, I am programming a windows service and all went well until I needed to use a simple array of chars which I initialize like this: char test = new char {'\x002F', '\x005E'}; Immediatly upon starting the service with this line in code (long before this line is even being used) I get a System.TypeLoadException which I cannot debug since it seems to happen somwhere deep in the
10
3023
by: wenmang | last post by:
hi, I have following: struct array1 { int id; char *name; }; struct array2 {
15
26431
by: thinktwice | last post by:
char a = { 0 } is it ok?
1
4367
by: BrandonG | last post by:
I am working on a JNI project, the env->NewStringUTF function will only accept a const char as a parameter. The trouble I am having is that within the native method I need to modify the string that is passed in and then return it. If i can initialize and put the data I need into a non-constant char then is there a way to create a const char that contians the data in the non-const one then pass the const array to the JNI function.
0
8756
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8667
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9251
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8961
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6585
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5922
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4429
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3122
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 we have to send another system

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.