Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a char*?

Tags:

c++

pointers

So I've been watching lectures from a University about C++, and I'm learning a lot, but one thing I still cannot understand is this:

Why do you have to do this sometimes?

char* test = "testing";

From what I've read/watched, I just don't understand why you have to put the *. From what I thought I understood, you only use * if you have an address, but maybe I'm just dead wrong.

like image 636
Austin Avatar asked Jul 25 '11 22:07

Austin


People also ask

What is a char * in C?

The abbreviation char is used as a reserved keyword in some programming languages, such as C, C++, C#, and Java. It is short for character, which is a data type that holds one character (letter, number, etc.) of data. For example, the value of a char variable could be any one-character value, such as 'A', '4', or '#'.

Is char * a string?

char* is a pointer to a character. char is a character. A string is not a character. A string is a sequence of characters.

What is a char * array?

A character array is a sequence of characters, just as a numeric array is a sequence of numbers. A typical use is to store a short piece of text as a row of characters in a character vector.

What does char * p mean in C?

In C programming language, *p represents the value stored in a pointer and p represents the address of the value, is referred as a pointer. const char* and char const* says that the pointer can point to a constant char and value of char pointed by this pointer cannot be changed.


2 Answers

This is a char:

char c = 't';

It can only hold one character!


This is a C-string:

char s[] = "test";

It can hold multiple characters. Another way to write the above is:

char s[] = {'t', 'e', 's', 't', 0};

The 0 at the end is called the NUL terminator. It denotes the end of a C-string.


A char* stores the starting memory location of a C-string.1 For example, we can use it to refer to the same array s that we defined above. We do this by setting our char* to the memory location of the first element of s:

char* p = &(s[0]);

The & operator gives us the memory location of s[0]. Here is a shorter way to write the above:

char* p = s;

Notice:

*(p + 0) == 't'
*(p + 1) == 'e'
*(p + 2) == 's'
*(p + 3) == 't'
*(p + 4) == 0  // NUL

Or, alternatively:

p[0] == 't'
p[1] == 'e'
p[2] == 's'
p[3] == 't'
p[4] == 0  // NUL

Another common usage of char* is to refer to the memory location of a string literal:

const char* myStringLiteral = "test";

Warning: This string literal should not be changed at runtime. We use const to warn the programmer (and compiler) not to modify myStringLiteral in the following illegal manner:

myStringLiteral[0] = 'b';  // Illegal! Do not do this for const char*!

This is different from the array s above, which we are allowed to modify. This is because the string literal "test" is automatically copied into the array at initialization phase. But with myStringLiteral, no such copying occurs. (Where would we copy to, anyways? There's no array to hold our data... just a lonely char*!)


1 Technical note: char* merely stores a memory location to things of type char. It can certainly refer to just a single char. However, it is much more common to use char* to refer to C-strings, which are NUL-terminated character sequences, as shown above.

like image 84
Mateen Ulhaq Avatar answered Oct 12 '22 22:10

Mateen Ulhaq


The char type can only represent a single character. When you have a sequence of characters, they are piled next to each other in memory, and the location of the first character in that sequence is returned (assigned to test). Test is nothing more than a pointer to the memory location of the first character in "testing", saying that the type it points to is a char.

like image 26
Mark H Avatar answered Oct 13 '22 00:10

Mark H