473,785 Members | 2,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

initialize char string

hi
is this correct

char teststr[100]="\0";

or is there another method to initialise;
thanks
lee
--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Nov 14 '05
10 43473
"Michael Mair" <Mi**********@i nvalid.invalid> wrote in message
news:37******** *****@individua l.net...


Mabden wrote:
"Michael Mair" <Mi**********@i nvalid.invalid> wrote in message
news:36******** *****@individua l.net...
>>char teststr[100] = {0};
>
>This one does not do the same thing. This one initializes the entire>array to zeros.

infobahn is right, gooch is wrong.

To clarify that: C99, 6.7.8#20 (Initializers)
"If there are fewer initializers in a brace-enclosed list than there
are
elements or members of an aggregate, or fewer characters in a string
literal used to initialize an array of known size than there are
elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage
duration."

That is, the second behaviour gooch was talking about (rest


initialised
to zero) is always what we can expect.

What does this do:

char teststr[100] = {1,2,3};

Does teststr now have 1, 2, 3 then 97 zeros? Or 1, 2, and 98 threes? Or what?


Think hard, mock elsewhere.


I was not mocking, this time.
Hint: Integer variables of static storage duration without an explicit
initialiser are initialised to 0, for arrays this applies elementwise.


I was wondering if the array is initialized with the last value or zero.
Actually I meant to add a third option (Does is init with
1,2,3,1,2,3,1,2 ,3,1,2,3 for the size of the array?) but I posted too
quickly and saved myself some embarrassment.. . ooops!

I thought the standard might have meant that it would take whatever
partial initializers it was given and propagated them to the entire
array. But it seems it actually treats it like an automatic variable and
fills with zeros.

I live and learn from all your wonderful insights.

--
Mabden
Nov 14 '05 #11

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

Similar topics

4
3775
by: Lingyun Yang | last post by:
hi every one, I meet a compile error in my small homework program: Can I innitialize ifstream with a string? or I must come back to char* style string? Thank you! Lingyun
74
43250
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); ----------
4
3512
by: qazmlp | last post by:
// Test.C Line-300: namespace Line-301: { Line-302: std::vector<std::string> vecaNS ; Line-303: } The 'SUN Forte 7 C++ Compiler' reports the following warning for the above code: "/advantage/hlri_tools/sol/SUNWspro/prod/include/CC/Cstd/./vector", line 318: Warning: should not initialize a non-const reference with a
6
2755
by: Kai Wu | last post by:
#include <string.h> #include <fstream> #include <time.h> typedef unsigned char BYTE; struct Dex { BYTE status; struct timeval timestamp; }; int main(){
9
27093
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
14
4459
by: cpisz | last post by:
I want to do some find and erase operations that i find in the string class, upon the entire text contents of a file. I made a function that will take a string (designed to hold the entire file contents) my problem now is how can i grab all the file contents in one swoop and put it in a string for editing? I hate to go through the file and copy line by line:
15
26436
by: thinktwice | last post by:
char a = { 0 } is it ok?
3
5788
by: windstorm | last post by:
for instance,I define a 2-D pointer: char **input = NULL; I want the "input" pointer to point to some string,and the number of the string is dynamic. when I got the first string,I was code like this: *input = strtok( row,token );
1
4373
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
9645
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
9481
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
10341
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
9954
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
7502
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
6741
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
5383
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
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.