derry30
derry30

Reputation: 241

Why is char *A able to hold strings while char A cannot?

I am having trouble understanding why a character pointer is able to hold a string.

Shouldn't it be like character, only should be able to hold a character.

Upvotes: 11

Views: 14010

Answers (8)

user13110365
user13110365

Reputation:

##char array or char pointer doesn't hold the string but only points to the Base Address##

A string is union of characters sequentially stored in Memory Locations. Thus if the memory address of the first char is known we can know the other addresses by incrementing (by the size taken by the data type. In this case it's 1 byte) the Memory Address.

A string is a char array and an array's name points to the memory address where the first character is stored (Base Address). String functions work by manipulating that Memory Address (not sure about that but I can't think of any other way).

On the other hand, a char pointer also points to the Memory Location of a character. Thus can be used the same way by string functions.

Thus strings can be declared using char arrays or char pointers...

Upvotes: 0

Artur
Artur

Reputation: 7257

char *a;

is a pointer(aka address/location expressed as number) to a character. It means that is does not hold a string (a sequence of characters) itself. In fact char *a's size (depending on various factors) is 4 bytes. Such a variable may point to actual location where your string really is (its first character followed by others and terminated by 0). In C/C++ it will be so called ASCIIZ ie. sequence of chars terminated by zero.

On the contrary this:

char a;

is a variable (usually 8bits) that stores just one character (plain ASCII char - not unicode codepoint etc)

Update: Of course MSalters below is right - the string may just contain anything ie. may be composed of any values that char can hold and this does not have (although usually is) to be interpreted as ASCII.

Upvotes: 1

MSalters
MSalters

Reputation: 179799

Picture:

+---+---+---+----+------
| A | B | C | \0 | ???
+---+---+---+----+------
  ^
  |---char*

Yes, each char* can point to only a single char at a time. But C++ strings like "ABC" are stored in memory as a contiguous sequence, without holes and with a 0 char at the end. Therefore, if you have the pointer to 'A', ++pointer will get you the pointer to 'B'. And you also know that you can do ++ until you find that last '\0'. (Which is exactly what strlen("ABC") does - use ++ 3 times to find the 0, so it returns 3.)

Upvotes: 9

Nick Louloudakis
Nick Louloudakis

Reputation: 6005

In C, (null terminated) strings are represented in memory in sequential memory cells of characters (their bytes - but I am not going to make it more complex on byte align etc.), with the last sequential character the '\0' (terminating):

char* str = "test"; -----> |t|e|s|t|\0|

char type is about one character only:

char ch = 'a'; -----> |a|

Now, in C++, you can either use C-style strings (char* types) or the string class of the standard library.

Upvotes: 0

RealityPC
RealityPC

Reputation: 137

Think string "abc" as a list of Char "a", "b" and "c", Char A is a variable of type Char, which cannot represent string "abc". Char *A is a pointer of type Char, it can point to a Char type variable. Char *A can be used to point to the first element of string, in this case, "a".

Upvotes: 0

UnholySheep
UnholySheep

Reputation: 4096

a character pointer does not hold anything except an address. This address is that of the first element of a char array (or can be at least). in essence char* is the same as char[]

A char on the other hand is a value type and cannot hold more than one byte.

Upvotes: 3

Plasmarob
Plasmarob

Reputation: 1391

it's actually simple:

char *A is a character pointer. it's another way of initializing an array of characters, which is what a string is.

char A, on the other hand, is a single char. it can't be more than one char.

See this conversation for more info:

http://www.cplusplus.com/forum/beginner/13343/

Upvotes: 0

SLaks
SLaks

Reputation: 887405

Char pointers are assumed to point to the beginning of a string.
The pointer itself points to the first character in the string, and code using the pointer assumes that the rest of the string follows it in memory, until it reaches a \0.

Upvotes: 20

Related Questions