Question:
what are char pointers or char*?
LAKERS 4 LIFE
2011-09-16 19:05:03 UTC
in c and c++ what the hell is char pointers or char*
Four answers:
oops
2011-09-16 19:10:43 UTC
It's a type of variable that holds the memory address of a char. Often, this char is the beginning of a char array. And usually a char array is used to hold a string, at least in C. In C++, we usually prefer to use the std::string class, because it is much more versatile.



Many functions are built around the idea that a char* points to the beginning of a string. It is convenient to treat them as such, since string literals, such as "Hello World", are actually char arrays, and char arrays will decay to char pointers when requested. That's what allows a function like printf to accept string literals when it's signature looks like this:



    int printf(const char * format, ...);



@Cur Bee:

In your example:



    cout << charPoint;



That does not print an address. If it was any type of pointer other than a char* it would, but char pointers get special treatment.
anonymous
2016-12-04 02:38:55 UTC
strcpy expects 2 information that should char char *strcpy(char *dst,char *src); Your first param isn't a pointer. it extremely is a char: curword is a char *, yet curword[i] is in basic terms a char. What you may desire there could be: &curword[i]. the 2d param is likewise in basic terms a char. yet right here you have a various situation, it extremely isn't any longer in an array. Taking the handle of it would not artwork right here, because of the fact strcpy expects strings to be null terminated, a single character isn't. there's a lots greater basic thank you to try this, you do no longer desire strcpy just to keep a single byte: curword[i] = tolower(c); And once you're completed, confirm curword is null terminated: curword[i] = ''0';
Cur Bee
2011-09-16 19:11:22 UTC
A char pointer is a variable that stores an address to a char variable.



char* charPoint=NULL;(pointer doesn't point to anything)

char charVariable='a';



cout << *charPoint; //dereference pointer, doesn't point to anything, will not work

cout << charPoint; //will work, gives the address it currently stores. Right now its a NULL character



charPoint=&charVariable; //pointer is set to the address of a character variable



cout << *charPoint; //now dereferenced, this output puts an 'a' on the screen

cout << charPoint; //this gives us the actual address of the variable

cout << &charVariable; //this does the same thing as above



cout << &charPoint; //this gives us the address of the variable that stores the address of a variable. Huehuehue
gh0st Rider
2011-09-17 02:47:06 UTC
@cur bee : to print the address of char pointer or char* , you will have to cast it to void



http://www.ideone.com/SCOg7


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...