LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-25-2009, 12:34 AM   #1
el_b
Member
 
Registered: Apr 2009
Distribution: Slackware 13.0
Posts: 48

Rep: Reputation: 17
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.
 
Old 09-25-2009, 01:05 AM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by el_b View Post
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.
 
Old 09-25-2009, 10:33 AM   #3
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
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.

Last edited by neonsignal; 09-25-2009 at 10:36 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with memset and char pointer naveenisback Programming 7 09-23-2009 04:33 PM
error: invalid conversion from const char* to char* Dahakon Programming 1 08-31-2009 09:33 AM
C string as an array of chars and as a pointer to char Alien_Hominid Programming 16 05-18-2009 08:22 AM
If I get invalid conversion from `const char*' to `char' what should I be lookin for? RHLinuxGUY Programming 5 03-12-2006 10:35 PM
is *- -p = '\n' valid? can i set \n char to a pointer? feetyouwell Programming 1 10-01-2004 01:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration