best use of strings

U

uidzer0

Hey everyone,

I'm a little confused on the best way to declare character strings.
I've seen many different ways to declare them such as:

---
char string[7] = "foobar\0";
---
char *string = "foobar";
--- and ---
char *string;
string = malloc(sizeof(char) * 7);
strcpy(string, "foobar");
free(string);
----

which one of these is the best? or is there a better option?

thanks!

Ben
 
K

Ken Human

uidzer0 said:
Hey everyone,

I'm a little confused on the best way to declare character strings.
I've seen many different ways to declare them such as:

---
char string[7] = "foobar\0";
---
char *string = "foobar";
--- and ---
char *string;
string = malloc(sizeof(char) * 7);
strcpy(string, "foobar");
free(string);
----

which one of these is the best? or is there a better option?

thanks!

Ben

It depends on what you want to do. char string[] = "foobar"; is a
character array of 7 characters including NUL. char *string = "foobar"
is a pointer to a string. The array can be changed, but the pointer
can't, unless it is changed to point to memory that you allocate, such
as in your third example. So, if you want an array of characters that
can't grow in size, the first is fine, if you want a string literal that
won't change while it's in scope, the second is fine, and if you want a
string that can change any time you need it to, use the third.
 
B

Barry Schwarz

uidzer0 said:
Hey everyone,

I'm a little confused on the best way to declare character strings.
I've seen many different ways to declare them such as:

---
char string[7] = "foobar\0";
---
char *string = "foobar";
--- and ---
char *string;
string = malloc(sizeof(char) * 7);
strcpy(string, "foobar");
free(string);
----

which one of these is the best? or is there a better option?

thanks!

Ben

It depends on what you want to do. char string[] = "foobar"; is a
character array of 7 characters including NUL. char *string = "foobar"
is a pointer to a string. The array can be changed, but the pointer
can't, unless it is changed to point to memory that you allocate, such

The pointer can be changed at will. However, whenever the pointer
points to a literal string, that string may not be modified.
as in your third example. So, if you want an array of characters that
can't grow in size, the first is fine, if you want a string literal that
won't change while it's in scope, the second is fine, and if you want a
string that can change any time you need it to, use the third.



<<Remove the del for email>>
 
K

Ken Human

Barry said:
uidzer0 said:
Hey everyone,

I'm a little confused on the best way to declare character strings.
I've seen many different ways to declare them such as:

---
char string[7] = "foobar\0";
---
char *string = "foobar";
--- and ---
char *string;
string = malloc(sizeof(char) * 7);
strcpy(string, "foobar");
free(string);
----

which one of these is the best? or is there a better option?

thanks!

Ben

It depends on what you want to do. char string[] = "foobar"; is a
character array of 7 characters including NUL. char *string = "foobar"
is a pointer to a string. The array can be changed, but the pointer
can't, unless it is changed to point to memory that you allocate, such


The pointer can be changed at will. However, whenever the pointer
points to a literal string, that string may not be modified.

You're right, that was a bad choice of words on my part. Thanks for the
correction.
 
T

Thomas Matthews

uidzer0 said:
Hey everyone,

I'm a little confused on the best way to declare character strings.
I've seen many different ways to declare them such as:

---
char string[7] = "foobar\0";
---
char *string = "foobar";
--- and ---
char *string;
string = malloc(sizeof(char) * 7);
strcpy(string, "foobar");
free(string);
----

which one of these is the best? or is there a better option?

thanks!

Ben

Actually I prefer:
const char * test_string = "foobar";

I declared a pointer to constant data. Hopefully the compiler
will catch any mistakes of writing to the literal.

--
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.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

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

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top