LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Why is char* a string and not a pointer to a char? (https://www.linuxquestions.org/questions/programming-9/why-is-char%2A-a-string-and-not-a-pointer-to-a-char-757650/)

el_b 09-25-2009 12:34 AM

Why is char* a string and not a pointer to a char?
 
This really annoyed me a while ago while I was studying C++ (from a book), why do you magically create a string INSTEAD of a pointer to a single char by using char*? It just seems illogical to me, i can't get over it.

I think I tried using it as you would use int* to an int and it did not work... If there is any way to use it like this please explain.

Thanks in advance.

lutusp 09-25-2009 01:05 AM

Quote:

Originally Posted by el_b (Post 3696407)
This really annoyed me a while ago while I was studying C++ (from a book), why do you magically create a string INSTEAD of a pointer to a single char by using char*? It just seems illogical to me, i can't get over it.

I think I tried using it as you would use int* to an int and it did not work... If there is any way to use it like this please explain.

Thanks in advance.

char *p = pointer to a character or array of characters (AKA "string"). If you increment the pointer, its address changes by the size of a char.

int *p = pointer to an int or an array of ints. If you increment the pointer, its address changes by the size of an int.

Seems logical and consistent to me.

Code:

#include <cstdio>

int main()
{
        char c = 'x';
        char *p1 = &c;
        printf("%c\n",*p1);
        const char *p2 = "this is a test";
        printf("%c\n",*p2);
        printf("%s\n",p2);
}

output:

Code:

x
t
this is a test

The only distinction between the two examples in the above program is that the string is assumed to be immutable, so it's declared "const" to satisfy the compiler.

neonsignal 09-25-2009 10:33 AM

Quote:

why do you magically create a string INSTEAD of a pointer to a single char by using char*?
The 'char *' doesn't magically create a string, it really is just a pointer (to a single character).

A string in C++ is not a fundamental type, like it is in some languages. Functions that work with strings use the idea of a null terminated sequence of characters, and the 'char *' argument is just the memory location of the first character in the sequence. The string manipulation functions take the pointer and increment it to step through the characters in the string.

The only strings that you will see in C++ code are the string constants (double quoted sequence of characters), which place the sequence of characters in memory with a null termination character, and have a value which is a pointer to the first character (ie, the start of the string).

It is important to realize that it is not the 'char *s' that creates the space for the string, but the string constant that it is being initialized to. Any pointer ('char *', 'int *', or whatever) that is not initialized will point to an undefined location.

Incidentally, C++ also has a standard template library that provides a dynamically allocated string type, which is a safer way to work with character strings than pointers in high level code.


All times are GMT -5. The time now is 10:21 AM.