char arrays and char*

what is a char* ? is it a pointer to a char array? how do I store input from a user into a char array? how do I initialize a char array?
char * is a pointer to one or more char.

if it is allocated with
char *cp = new char; //one character.
char *cp = new char[10]; //array of 10 characters.

more precisely

char* is usually an integer type that contains the address in ram where your data is stored, whether that address is an offset to a known location or a pure address depends on operating system and things out of your control. Just know that it is a way to access the memory that you have obtained from the operating system.

these are NOT char arrays. They can ACT exactly like a char array, but they are not 100% the same things.

however, whether you do this:
char ca[10];
or this
char *cp = new char[10];

you can work with them more or less the same ways.

cin << ca;
cin << cp; //virtually identical. This takes user input into the variable. Warning, it can't handle more than 9 input characters in this case, or you will have a potential crash or other problems due to accessing memory outside the allocated amount.

all your C string functions work on either one..
strcat, strlen, sprintf, printf, etc all accept both with the same syntax. It is preferred to use the string type instead, in c++ for text. There are times to use an array of char as "bytes" for low level code (talking to a serial or Ethernet port, for example). Even there, vectors are usually preferred over arrays. Basically, arrays are not quite obsolete and char arrays as strings pretty much are obsolete.


initializations are not identical though.

char ca[10] = {0}; //empty string, all elements are 0 which is also the end of string marker character.

char ca[10] = "word"; //initialize to text.

char *cp = 0; //null pointer. you can't do anything to it, there is no memory assigned. The is the same as null or nullptr constants on almost all systems but it is preferred to use the named value nullptr in c++.

char *cp = new char[10]; //gets memory. You can't initialize a value here.
cp[0] = 0; empty string.
sprintf(cp, "%s", "word"); //value.


you can do this:

char *cp = new int[10](); //but I can't recall what it actually does.



Last edited on
> what is a char* ? is it a pointer to a char array?

The type char* is 'pointer to char'

The type char[10] is 'array of 10 char'

The type char(*)[10] is 'pointer to array of 10 char'


> how do I store input from a user into a char array?

1
2
3
4
5
6
7
8
9
char cstr[25] ; // array of 25 char

std::cin >> std::setw(25) >> cstr ; // skip leading white space, stop at white space
                              // read up to a maximum of 24 characters into cstr

std::cin.getline( cstr, 25 ) ; // do not skip leading white space, stop at new line
                  // read up to a maximum of 24 characters (including white space) into cstr

// in either case, null-terminate cstr 



> how do I initialize a char array?

1
2
3
4
5
6
7
char cstr[25] {} ; // initialise with all zeroes

char cstr2[] = "abcde" ; // figure out the size of the array (6); 
                         // initialise with the six characters 'a', 'b', 'c', 'd', 'e', null 

char cstr3[100] = "abcde" ; // initialise the first six characters with 'a', 'b', 'c', 'd', 'e', null 
                                           // zero-initialise the remaining characters 
oh okay I get it now but how do I get the input from user and store it into a char* but shorten the input to like a length of 14 so that I don't get a memory error?
can I make the char* like dynamic so that I don't have to make a set length?
Last edited on
You can do this:
char *cp = new int[10]();

Probably you mean
char *cp = new char[10]();, which value-initializes each element of the array. (The first version doesn't compile.)
http://en.cppreference.com/w/cpp/language/value_initialization
Last edited on
We can't store a string (a sequence of characters) in a pointer; an array is required; we need an array.

The array can be dynamic, but that is a bad idea.

The simplest solution is to use std::string https://cal-linux.com/tutorials/strings.html

Another reasonable option is to have an array with a size larger than required, and limit the number of characters that would be read in. (See the example in the earlier post.) We don't have to worry about shortening the array because the null character at the end of input indicates the logical end of a c-style string. eg.
1
2
char cstr[100] = "abc" ; // array of hundred characters; the first four are 'a', 'b', 'c', null
std::cout << cstr ; // print the three characters 'a', 'b' and 'c' 
Topic archived. No new replies allowed.