tags:

views:

528

answers:

8

Studing the code in Schaum's C++ book, i saw a lot of code using char*, int* etc. Doing the exercises i also saw that in the solutions there is char* and in my code i have used char (without the star).

I want to know what is the difference between a char and a pointer char - integer and a pointer integer ? Where should i use them ? What is exactly their meaning ?

+6  A: 

The variables with the * are pointers.

A 'normal' variable, for example a char or an int, contains the value of that datatype itself - the variable can hold a character, or an integer.

A pointer is a special kind of variable; it doesn't hold the value itself, it contains the address of a value in memory. For example, a char * doesn't directly contain a character, but it contains the address of a character somewhere in the computer's memory.

You can take the address of a 'normal' variable by using &:

char c = 'X';
char * ptr = &c;  // ptr contains the address of variable c in memory

And you get the value in memory by using * on the pointer:

char k = *ptr;  // get the value of the char that ptr is pointing to into k

See Pointer (computing) in Wikipedia.

Jesper
I know what pointer is - i want to know the difference between a normal variable and a pointer, and why in some cases i should use pointer instead of a normal variable.
gujo
If you know what a pointer is, then you know the difference. You obviosly don't. It's not that simple, and even after a long time can the big "ahha!" moment come.
Tamás Szelei
If you know what a pointer is, and you know what an int or a char is, then you also know what the difference is between int and int* or char and char*. If you don't know the difference between them, then you don't know what they are.
jalf
A normal variable is like a house, and a pointer is the address to the house. The address points to a physical house.
James Black
A: 

With a char or int, the variable value is the actual assigned value - 'a', or 42. With a char* or int*, the variable value is the location in memory where the actual assigned value is saved. Hence, the * means that the variable points to a location in memory where the value can be found.

Kaleb Brasee
+2  A: 

A pointer points to the memory address of a specific variable. Pointers can be quite hard to understand at first for some people, more information about it on wikipedia.

A char* is a pointer to a sequence of characters in memory, ended with a '\0'. A single char represents one character. An int* holds the memory address to an integer value.

Example:

int* x = new int();

We create a new integer variable on the heap, and the location in memory is saved in the pointer. The pointer now points to that part of the memory where the integer is located. If I would like to use the value of the integer that the pointer points to, then I would call:

int y = *x;

This dereferences the memory address; it gets the value behind the pointer. The value of y becomes the value of the data type behind the memory the pointer points to.

Chaoz
I had to buy a book on C that was just on pointers, but once I understood them then C became easy to program in.
James Black
+5  A: 

You should read chapter 7 in your book which explains pointers - you will probably need more than one short answer here to really understand the subject.

Georg Fritzsche
+1  A: 

When you have char or int or long or anything like that, then memory is set aside to hold the value, so, a char is 1 byte of memory for storage.

The address of that byte is what the pointer points to.

This is helpful when you have a string, or a contiguous row of char, as you can then point to the start of that string, and pass it to another function, so that function can use the actual values in the string.

So, you signify the pointer by using the asterisk.

James Black
A: 

Variables with * are pointer. This means that they are containing a memory address whereas variables without * contains value.

So a char contains one character value like 'a' or 'Z'. char* points to a memory zone where you can access values char by char.

A int variable contains one integer value like 1 or 12345. int* points to a memory zone where you can access values int by int.

This is true for all other types.

Example:

// pointer declaration
char* pchar = new char[5];

THis allocates a memory zone of 5 * sizeof(char) and allow you to access 5 characters.

Don't forget to release memory when your pointer is not needed anymore.

// pointer destruction
delete pchar;
Patrice Bernassola
Your example does not allocate an array of 5 chars. It allocates one char with the value 5.
erikkallen
Oops use '()' instead of '[]'. Thank you to point it to me
Patrice Bernassola
A: 

OK,

I'll take a stab at this. The difference between char and char* is where the compiler puts the variable in memory that your using.

char c is a stack declaration. The container holds the value of the data you put into it, and stack scope rules apply. When you hit the end of your method body, c would "go out of scope" and be automatically cleaned up.

char *c is a pointer variable. Meaning that, the variable doesn't hold the value of the data you put into it, but rather an address to a place in memory instead, commonly referred to as its, "memory address". Then we say that c is "on the heap" and you, the programmer are responsible for cleaning it up. So when c goes out of scope it isn't automatically cleaned up, you are required to call delete on it to free the memory.

The *, is a unary operator and also referred to as the deferencing operator.

here's a good wiki Dereference_operator

Chris
Pointers do not necessarily point to memory on the heap. See Jesper's answer for an example of a pointer to stack memory.
Chuck
A: 

char is a value type, so referencing a variable of that type gets a char. E.g.,

char ch = 'a';
cout << ch; // prints a

char* is a pointer to a char, typically used for iterating through strings. E.g.,

char* s = "hello";
cout << s; // prints hello
s++;
cout << s; // prints ello
UncleO