How to initialize a char*?

I

Ioannis Vranos

Julie said:
???

Sure you can -- a reference to a variable of type char *.


I do not understand what your point is. To whom are you referring to by
"his"?


Can you provide a working example with a main() using such a function
accepting char * &?






Ioannis Vranos
 
I

Ioannis Vranos

Matt Wharton said:
This is true, but I think it would be fair to argue that it is always good
practice to initialize variables to something (most likely NULL or 0 in this
case). No harm comes from doing this (always initializing), whereas there
are many situations where NOT initializing will shoot you in the foot.


An example?

To make long things short, initialization doesn't protect you from anything.
The only thing that it could "protect you" in theory is in the call of
delete/delete[] family to an ititialised-to-0 pointer. In reality however
such a call occurs at the end of a process and you will have done more
things previously by mistake on that 0 initialised pointer (e.g.
dereferencing it) that would be equally disastrous/undefined.

Initialisation does not protect you from any mistake.






Ioannis Vranos
 
I

Ioannis Vranos

Kevin Goodsell said:
Never use any variable before you've given it a meaningful value. Doing
so gives undefined behavior, and could crash your program or worse.

You reminded me a humorous text that i had read a long ago which i just
searched in google. Check it:

http://www.lysator.liu.se/c/ten-commandments.html


Of course it has nothing to do with the subject, but this "never" of yours
reminded it to me. :)


This uses a dangerous and deprecated language feature -- the implicit
conversion of a string literal to a (non-const) char*. I'd suggest
making your compiler warn about this if it provides such a warning.
Eh?


Initialize it with whatever you need. You're the only one who knows what
purpose it serves, so we can't suggest what to use. But leaving it
uninitialized is an error, and doesn't make sense.

It is not an error!






Ioannis Vranos
 
J

Julie

Ioannis said:
I do not think it is. You can take references from pointers only in
templates. Even in this case passing an unitialised char * would not have
any bad effect by its own.

Reference to a pointer type is perfectly legal, and has nothing to do w/
templates.

If you feel that I'm wrong, please correct me by referencing the C++ standard.
Nope. If he chooses to make the unneeded initialisation he should use 0, NOT
NULL.

NULL is perfectly acceptable, and is standard.
 
J

Julie

Ioannis said:
Can you provide a working example with a main() using such a function
accepting char * &?

How about you go ahead and try it, and learn for yourself.
 
J

Julie

David said:
On Tue, 13 Apr 2004 15:47:58 -0700 in comp.lang.c++, Julie
Yes, my statement was directed at the 'evil' part -- there is nothing 'evil'
about it, it just has the potential to lead to undefined behavior.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[6.14] What does the FAQ mean by "such-in-such is evil"?".
http://www.parashift.com/c++-faq-lite/

Thanks for pointing it out.

After reading, that section definitely comes off as infantile and
inappropriate. I'd much prefer a more precise (and less humanity-subjective)
term as 'evil'. But I didn't write that FAQ, nor have any plans to write one,
so MC is free to write what he wants. Regardless, that doesn't preclude me
from stating that 'evil' is a poor word choice when discussing programmatic
constructs and behaviors.
 
J

Julie

Ioannis said:
Not absolutely.

Yes absolutely.

From now on, in order to be considered a productive participant, you may want
to include more than simple phrase answers and actually provide some insight
into the matter.
 
I

Ioannis Vranos

Julie said:
How about you go ahead and try it, and learn for yourself.


Why don't you try? Just provide an empty definition of a function getting
char *&), create a main() with a valid call to it and if it compiles post it
here (i am not kidding you will see).






Regards,

Ioannis Vranos
 
J

Julie

Ioannis said:
Matt Wharton said:
This is true, but I think it would be fair to argue that it is always good
practice to initialize variables to something (most likely NULL or 0 in this
case). No harm comes from doing this (always initializing), whereas there
are many situations where NOT initializing will shoot you in the foot.

An example?

To make long things short, initialization doesn't protect you from anything.
The only thing that it could "protect you" in theory is in the call of
delete/delete[] family to an ititialised-to-0 pointer. In reality however
such a call occurs at the end of a process and you will have done more
things previously by mistake on that 0 initialised pointer (e.g.
dereferencing it) that would be equally disastrous/undefined.

Initialisation does not protect you from any mistake.

That is true.

Perhaps it would be better to term this as 'zero initialization', after all

char * ptr = (char *)12345;

is initialized, but not much better than leaving it uninitialized.
 
B

Buster

Julie said:
NULL is perfectly acceptable, and is standard.

Yes. It's defined in the clocale, cstddef, cstdio, cstdlib, cstring,
ctime and cwchar headers. In my opinion it's preferable to use 0
because it's supported directly by the language.
 
B

Buster

Ioannis said:
Why don't you try? Just provide an empty definition of a function getting
char *&), create a main() with a valid call to it and if it compiles post it
here (i am not kidding you will see).

Ioannis, post one that doesn't compile. Your problem is elsewhere.
 
J

Julie

Ioannis said:
Why don't you try? Just provide an empty definition of a function getting
char *&), create a main() with a valid call to it and if it compiles post it
here (i am not kidding you will see).

Regards,

Ioannis Vranos

I did this for myself long ago, and it compiled and worked just fine.

Feel free to try for yourself.
 
I

Ioannis Vranos

Julie said:
NULL is perfectly acceptable, and is standard.


"The C++ Programming Language" 3rd Edition or Special Edition by Bjarne
Stroustrup (the creator of C++), page 88:



5.1.1 Zero [ptr.zero]

Zero (0) is an int. Because of standard conversions (§C.6.2.3), 0 can be
used as a constant of any
integral (§4.1.1), floatingpoint, pointer, or pointer to member type. The
type of zero will be determined
by context. Zero will typically (but not necessarily) be represented by the
bit pattern allzeros
of the appropriate size.

No object is allocated with the address 0. Consequently, 0 acts as a pointer
literal, indicating
that a pointer doesn't refer to an object.

In C, it has been popular to define a macro NULL to represent the zero
pointer. Because of
C++'s tighter type checking, the use of plain 0, rather than any suggested
NULL macro, leads to
fewer problems. If you feel you must define NULL, use

const int NULL = 0;

The const qualifier (§5.4) prevents accidental redefinition of NULL and
ensures that NULL can be
used where a constant is required.



By the way it is an excellent book, i have been reading it on my own. I
strongly suggest it.






Ioannis Vranos
 
I

Ioannis Vranos

Buster said:
Ioannis, post one that doesn't compile. Your problem is elsewhere.


void func(char * const input, int word_number, char * & ptr_to_word)
{
}


int main()
{
char array[]="b";


func("a",5, array);
}






Ioannis Vranos
 
J

Julie

Ioannis said:
Buster said:
Ioannis, post one that doesn't compile. Your problem is elsewhere.

void func(char * const input, int word_number, char * & ptr_to_word)
{
}

int main()
{
char array[]="b";

func("a",5, array);
}

Ioannis Vranos

Yes, your problem is elsewhere.

The function takes a reference argument of type char *, you are (attempting) to
pass a type of char[2].

Here is your hat and coat.
 
I

Ioannis Vranos

Julie said:
That is true.

Perhaps it would be better to term this as 'zero initialization', after all

char * ptr = (char *)12345;

is initialized, but not much better than leaving it uninitialized.


Ok, so zero-initialisation or any other "premature" initialisation does not
protect you from anything. Btw try using the new C++ casts instead (of
course avoid them completely if possible).






Regards,

Ioannis Vranos
 
I

Ioannis Vranos

Buster said:
void f (char * &) { }
int main ()
{
char c;
char * p = & c;
f (p);
}



But this doesn't compile:

void f (char * &) { }

int main ()
{
char c;
f (&c);
}






Ioannis Vranos
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top