C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: char * vs char[]

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Julián Albo
Guest





PostPosted: Wed Jun 25, 2003 6:21 pm    Post subject: Re: char * vs char[] Reply with quote



C Wood escribió:

Quote:
void some_func() {
char temp[] = "prepend"; /*This one*/
char *temp= "prepend"; /*Or this one*/
strcat(temp," before this");
}

None of them. char temp []= "prepend"; allocates just the space needed
for the characters of the string plus the zero terminator. If you strcat
to it you are out of space.

Regards.

Back to top
John Harrison
Guest





PostPosted: Wed Jun 25, 2003 6:25 pm    Post subject: Re: char * vs char[] Reply with quote




"C Wood" <cdotrdotwood (AT) verizondotnet (DOT) removeme> wrote

Quote:

Dear group,

I forgot which declaration is constant. Intentions are:

void some_func() {
char temp[] = "prepend"; /*This one*/
char *temp= "prepend"; /*Or this one*/
strcat(temp," before this");
}

Thanks...


The second, because all you've declared is a pointer which points at a
string literal, and string literals are read-only.

Despite appearances the first doesn't involve a string literal, its a
confusing short hand for this.

char temp[] = {'p', 'r', 'e', 'p', 'e', 'n', 'd', '' };

Obviously if you declare a non-const array, you can modify it.

Your strcat is illegal in both cases, nothing to do with const, instead you
are writing over memory you haven't allocated in any way.

john



Back to top
Thomas Matthews
Guest





PostPosted: Wed Jun 25, 2003 6:45 pm    Post subject: Re: char * vs char[] Reply with quote



C Wood wrote:
Quote:
Dear group,

I forgot which declaration is constant. Intentions are:

void some_func() {
char temp[] = "prepend"; /*This one*/
char *temp= "prepend"; /*Or this one*/
strcat(temp," before this");
}

Thanks...



In either case the literal text "prepend" is constant.
Also, you can't have a variable declared with two
different types.

The first declaration declares an array of char.
The array is initialized with the characters from
the text literal. The size of the array is determined
by the length of the literal.

In the second declaration, you are declaring a pointer
to char which initialized to point to the literal.
The pointer can be modified. If the target of the
pointer is modified, undefined behavor is invoked.

The second declaration causes the literal to have
a physical address. In the first declaration, the
literal doesn't have to have a physical address;
the literal can disappear after the declaration
is executed.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


Back to top
C Wood
Guest





PostPosted: Wed Jun 25, 2003 6:53 pm    Post subject: Re: char * vs char[] Reply with quote


"Thomas Matthews" <thomas_matthews (AT) sbcglobal (DOT) net> wrote

: C Wood wrote:
: In either case the literal text "prepend" is constant.
: Also, you can't have a variable declared with two
: different types.
:
: The first declaration declares an array of char.
: The array is initialized with the characters from
: the text literal. The size of the array is determined
: by the length of the literal.
:
: In the second declaration, you are declaring a pointer
: to char which initialized to point to the literal.
: The pointer can be modified. If the target of the
: pointer is modified, undefined behavor is invoked.
:
: The second declaration causes the literal to have
: a physical address. In the first declaration, the
: literal doesn't have to have a physical address;
: the literal can disappear after the declaration
: is executed.
:
I know, I tossed a fast sample using syntax I don't normally use and
totally wrote trash uncompilable code.

What I really wanted was a better way than this:

void func() {
char temp[8];
temp[0] = 'P';
temp[1] = 0;
strcat(temp,"asdf");
}

Application is tossing in drive letters and such before filenames, in a
clean and readable manner. Why worry about what works? It just annoying to
do it the slow way, when it seems it could be preinitiailized. Probably
worried about something that makes no difference anyways. Premature
opt.............l.

That way the temp would be:

"Pasdf"

Looks like the method:

char temp[8] = {'P',0,0,0,0,0,0,0};

is the best, or the way I've been doing it is fine also.

Back to work it is then...........






Back to top
Corey Murtagh
Guest





PostPosted: Wed Jun 25, 2003 7:15 pm    Post subject: Re: char * vs char[] Reply with quote

C Wood wrote:
<snip>
Quote:

Looks like the method:

char temp[8] = {'P',0,0,0,0,0,0,0};

is the best, or the way I've been doing it is fine also.

Back to work it is then...........

Err...

char temp[8] = "P";

or

char temp[8] = {'P', 0};

Works just as well, except I can't guarantee that the rest of temp past
the first 2 bytes will be initialized to anything useful (like nuls for
example). Saves on all that typing of nuls at least.

--
Corey Murtagh
The Electric Monk
"Quidquid latine dictum sit, altum viditur!"


Back to top
Ron Natalie
Guest





PostPosted: Wed Jun 25, 2003 7:33 pm    Post subject: Re: char * vs char[] Reply with quote


"Corey Murtagh" <emonk (AT) slingshot (DOT) co.nz.no.uce> wrote


Quote:

char temp[8] = {'P', 0};

Works just as well, except I can't guarantee that the rest of temp past
the first 2 bytes will be initialized to anything useful (like nuls for
example). Saves on all that typing of nuls at least.

Actually all you need is
char temp[8] = { 'P' };

The other seven spots are default initialized (zero'd).
If you specify fewer initializers than the size of the array, the rest are
default initialized.



Back to top
Thomas Matthews
Guest





PostPosted: Fri Jun 27, 2003 2:50 pm    Post subject: Re: char * vs char[] Reply with quote

C Wood wrote:
Quote:
"Gavin Deane" <deane_gavin (AT) hotmail (DOT) com> wrote in message
news:6d8002d0.0306251633.19d47d26 (AT) posting (DOT) google.com...
: "C Wood" <cdotrdotwood (AT) verizondotnet (DOT) removeme> wrote in message
news:XcmKa.12914$Xb.2040 (AT) nwrddc02 (DOT) gnilink.net...
: Any reason why std::string doesn't work for you?
: GJD

The reason is that compiler extensions call this function. Any
discussion of that is Off Topic here.


Here is an example of using string and some functions/methods that
require char *:

#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

const string file_name = "my_data.txt";
int main(void)
{
ofstream my_file(file_name.c_str());
if (my_file)
{
my_file << "Hello." << endl;
cout << "File written." << endl;
}
else
{
cout << "Error opening file." << endl;
}
return EXIT_SUCCESS;
}

I would hope that in the next iteration of the standard,
they allow std::string as a parameter to standard methods
and functions that require char *.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


Back to top
Rayiner Hashem
Guest





PostPosted: Fri Jun 27, 2003 8:16 pm    Post subject: Re: char * vs char[] Reply with quote

"C Wood" <cdotrdotwood (AT) verizondotnet (DOT) removeme> wrote

Quote:
Dear group,

I forgot which declaration is constant. Intentions are:

void some_func() {
char temp[] = "prepend"; /*This one*/
char *temp= "prepend"; /*Or this one*/
strcat(temp," before this");
}

Thanks...
If you have the STL available (and you should, you can use std::string

in an OS kernel without any more runtime support than malloc/free Smile,
you can do this:

char* some_func()
{
std::string str("prepend");
str += " before this";
return strdup(str.c_str()); // this will give the caller a copy of
the combined string
}

Even if you get one part or the other of the string as straight
C-strings, you can use the appropriate constructors for std::string to
handle the conversion under the hood.

Back to top
Ron Natalie
Guest





PostPosted: Fri Jun 27, 2003 9:06 pm    Post subject: Re: char * vs char[] Reply with quote


"Rayiner Hashem" <heliosc (AT) mindspring (DOT) com> wrote

Quote:
return strdup(str.c_str()); // this will give the caller a copy of

strdup is not a standard function.



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.