Pointres question

Hi I am totaly new at C++ programming.I have a question about pointers.
when you declare a pinter you do something like this

int *a;

If you dont write the * symbol whats the difference?for example its only an int and if you have the * its both and and pointer?
I would appreciate it if you could help me with this question.
Also an example would be very helpful.
When you put the asterisk ( * ) then you declare a pointer. In your example it is a pointer to an int. Not an int. You can make a new int from it by using the new command but it will always be a pointer.
Maybe the tutorial can help you more: http://www.cplusplus.com/doc/tutorial/pointers.html
Last edited on
char a makes 'a' be of size 1. An 8-bit integer. char *a makes 'a' of pointer size (the size depends on the system. On an x86, the size is 4). A pointer is nothing more than a memory address.
When uninitialized, the value of pointer is undefined (http://en.wikipedia.org/wiki/Undefined_behavior). It will normally point to some invalid location, but this is not guaranteed, so you should initialize pointers as soon as possible (at least before dereferencing them). The meaning of the * operator varies according to where it's used (the comments belong to the code that precedes them):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
int *a,
     b;
/*
This defines a pointer to an integer and an integer (b IS NOT A POINTER).
When in a declaration, * acts as syntax (it's not quite an operator) and
declares the identifier as a pointer to the type to its left. So 'int' is an
integer, 'int *' is a pointer to an integer, 'int **' is a pointer to a pointer
to an integer, etc.
*/
int *c,
    *d;
//In this case case, both c and d are pointers.
std::cout <<a<<std::endl;
/*
OK. Although a is uninitialized, it still has a value. Some environments
(Visual C++, for instance), however, will complain if you try ro get the value
from an uninitialized variable. If it doesn't, this will print a hexadecimal
number like 0xABCDEF01.
*/
std::cout <<*a<<std::endl;
/*
This is dereferencing. When the * operator is in an expression, it acts as the
dereference operator, and gives the variable or object at the memory address
the pointer points to.

However, this is incorrect because a is uninitialized and points to an unknown
(and probably invalid). The least that can happen is that you get garbage. In
this case, the worst is that you produce a segmentation fault because you tried
to access memory outside of your segment (the memory allocated for your
program).
*/
(*a)++;
/*
This, on the other hand, is very dangerous. You'll be lucky to get a
segmentation fault. If you don't, you could be writing to an unknown address
with unpredictable results.
The general rule is: is you don't initialize pointer immediately, set them to
zero or NULL (if (!pointer) or if (pointer == NULL)) so that you can easily test
if they're valid. Never dereference invalid pointers.
*/
a=new int;
/*
Now a points to a valid memory address. I can't remember ATM if the value
pointed to is initialized or not. Let's suppose it's initialized to zero.
*/
std::cout <<*a<<std::endl;
//And now, this prints a zero.
(*a)++;
//And this is also valid now. a now points to a 1. 


EDIT: Damn. Was I too verbose?
Last edited on
First of all thanks for the useful posts.but this is not what I asked.Probably the mistake is mine.
I wanted to know,if I have the following code
1
2
int *a;
int b;


what extra operations can be applied to a?
The asterisk operator can be applied to b or no?to conclude if a variable is declared as pointer it can be act both as pointer and int(in our example)?

Basically the following code from the tutorial confused me a bit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// my first pointer
#include <iostream>
using namespace std;

int main ()
{
  int firstvalue, secondvalue;
  int * mypointer;

  mypointer = &firstvalue;
  *mypointer = 10;
  mypointer = &secondvalue;
  *mypointer = 20;
  cout << "firstvalue is " << firstvalue << endl;
  cout << "secondvalue is " << secondvalue << endl;
  return 0;
}


mypointer is used both with and without asterisk...
1. Dereference (e.g. b=*a). Also, pointer arithmetic is applied. (e.g. If __int32 *a=0, and then you do a++, then the value of a is now 4, not 1.)
2. No. You can't be sure that sizeof(int)==sizeof(void *).
3. Yes, but it's not practical and that's not what pointers are used for. And due to pointer arithmetic, some operations may give unintuitive results.
Pointers are great. Its what makes C so superior.

Don't fret over the asterisk, because mypointer is declared as a pointer to a number and not a number itself.
If your uncomfortable with pointers just use the asterisk when you've already pointed to a integer and you want to change the value of the integer being pointed at.
When you want to point to something else don't use the asterisk use the =&pointedTo.

Theres alot more to pointers than this, they do a whole lot and its difficult to get used to them. I had alot of trouble with them.
A pointer is a variable that hold memory addresses of other variables it is pointing to.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 // my first pointer
#include <iostream>
using namespace std;

int main ()
{
  int firstvalue, secondvalue; //variables (int)
  int * mypointer;                  //pointer to (int)

  mypointer = &firstvalue;
  *mypointer = 10;
  mypointer = &secondvalue;
  *mypointer = 20;
  cout << "firstvalue is " << firstvalue << endl;
  cout << "secondvalue is " << secondvalue << endl;
  return 0;
}


In line 7 - you are declaring 2 integer variables that are going to hold integer values.

In line 8 - you are declaring a pointer that is going to point to (hold the memory address of) an integer variable.

In line 10 - '&' is the reference operator. As such you are are storing the memory address of the variable firstvalue in mypointer.

In line 11 - '*' is called the dereference or the 'value at' operator. What you are basically doing here is that you are assinging the value 10 to the memory address stored in mypointer. Since the memory address is that of firstvalue (see line 10), firstvalue will now have 10.
Therefore cout<<firstvalue will give you 10

The same is happenening in lines 12 and 13.

Hope that helped.
Last edited on
Topic archived. No new replies allowed.