confusion with char and pointer char

closed account (4ET0pfjN)
Hi, I am a little confused with difference and usefulness of:
char list[10] vs char *list

Is is that the first you must declare size of array (c-string is that what this is) during compile time whereas the second is dynamic array (dynamic c-string) so that we can declare during run time which is better (no wasted memory allocated)?

Any help appreciated.
The first is an array of 10 characters, which may or may not represent a null-terminated character sequence.

The second is a pointer to a modifiable character, which may or may not be an element of some array of characters, which may or may not represent a null-terminated character sequence.

There isn't much to compare unless you have code samples which demonstrate the intended use. If your goal is to work with strings, use std::string.
closed account (4ET0pfjN)
I'm new to C++ (I'm more experienced with Java and PHP), so can you show me some examples to demonstrate when and why I would want to use each?

Also, why do you keep emphasizing null-terminated character sequence, is that important to have?
For get strings for a while.

char list[10]; is an array of 10 chars (size is 10 bytes).

char *list; is a pointer to a char (size is typically 4 bytes).

The C programming lanuage didn't really have a string type. But it was decided to implement string as a sequence of characters followed by character zero, which we call null. The language syntax was tweaked to support the initialisation of strings, and a set of string functions added as a library.

char list[10]; = "me"; is an array of 10 chars. list[0]='m', list[1]='e', the rest have zero. You read/write the content of this array as much as you like.

char *list = "me"; "me" is a three character array set up somewhere in memory ('m', 'e', zero) and list points to the first element. You should never try to overwrite the content of such a string as you don't know where the string actually lives.
I don't know of such examples, at least not what would be needed to someone new to the language -- I use char* sometimes to access binary representations of trivially-copyable objects, but I doubt that's what you're looking for. If your goal is to work with strings, use std::string.

if a sequence of valid single or multibyte characters is null-terminated, certain library functions process it as a string as well, that's what is broadly called "c-strings".

@kbw: char *list = "me"; is invalid C++ (was deprecated C++ until last year)
Last edited on
I suppose char* is used where we dont know the size of the string .. as
char name[50];
 //here we are assuming that the name will not extent more then 50 character.


1
2
3
4
5
int size = getsomecalulationforsize();
char* statement  = new char[size];
.....
....
delete [] statement ; 

here the size depends upon the calculation of the function .. which is calculated at the runtime.
Nah, if you don't know the size, you'll use std::string ;)
you are right ne555.. but what if we what to use char instead of string .
You will end emulating it. ¿How will you read an arbitraly long character sequence?
I have only ever used character arrays when I need an array of values that don't exceed the range -128 to 127 or 0 to 255. Sometimes, though, I use them to interact with low-level functions that want me to give them a pointer to the first character in a string that ends with a null character.
Topic archived. No new replies allowed.