473,770 Members | 5,426 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 #1
10 43472
Yang Lee wrote:
hi
is this correct

char teststr[100]="\0";

or is there another method to initialise;


char teststr[100] = "" suffices, because the string literal already has a
NUL character at the end (in position 0, in this case).
Christian
Nov 14 '05 #2
Yang Lee wrote:

hi
is this correct

char teststr[100]="\0";
It works.
or is there another method to initialise;


The following both do the same thing, perhaps a touch more elegantly:

char teststr[100] = "";
char teststr[100] = {0};
Nov 14 '05 #3

infobahn wrote:
Yang Lee wrote:

hi
is this correct

char teststr[100]="\0";
It works.
or is there another method to initialise;


The following both do the same thing, perhaps a touch more elegantly:

char teststr[100] = "";

This initializes the first element to a terminating null character and
does nothing to the rest of the array. char teststr[100] = {0};

This one does not do the same thing. This one initializes the entire
array to zeros.

Nov 14 '05 #4
gooch wrote:

infobahn wrote:
Yang Lee wrote:

hi
is this correct

char teststr[100]="\0";


It works.
or is there another method to initialise;


The following both do the same thing,
perhaps a touch more elegantly:

char teststr[100] = "";

This initializes the first element to a terminating null character and
does nothing to the rest of the array.
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.

--
pete
Nov 14 '05 #5

pete wrote:
gooch wrote:
infobahn wrote:
Yang Lee wrote:

hi
is this correct

char teststr[100]="\0";

It works.
or is there another method to initialise;

The following both do the same thing,
perhaps a touch more elegantly:

char teststr[100] = "";


This initializes the first element to a terminating null character and
does nothing to the rest of the array.
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.
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #6
infobahn is right, gooch is wrong.

--
pete


I stand corrected. I seem to remember reading this in the standard at
one point but I went to my document after reading your reply and there
it is just as you say.

Nov 14 '05 #7
"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?

--
Mabden

Nov 14 '05 #8
In article <RZ************ ***@newssvr13.n ews.prodigy.com >,
Mabden <mabden@sbc_glo bal.net> wrote:
"Michael Mair" <Mi**********@i nvalid.invalid> wrote in message
news:36******** *****@individua l.net...
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?


Its 1, 2, 3 then 97 zeros. It uses the values provided and then fills the
rest of the array will zeros.

Kevin

Nov 14 '05 #9


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.
Hint: Integer variables of static storage duration without an explicit
initialiser are initialised to 0, for arrays this applies elementwise.
-Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #10

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

Similar topics

4
3773
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
43236
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
3510
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
27088
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
4458
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
26435
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
9432
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
10059
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7420
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
6682
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
5313
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...
1
3974
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
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2822
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.