Skip to Content

Pointers, the short version!

Actually, pointers are not exactly references. A pointer is a normal variable but it carries the memory address of another variable. That other variable can be a premitive type (char, int, bool, long, ..) or an aggregate type (Object, structure, array, ...) or a function (pointers to functions).

A point could point to anything (reference anything), that could be a pointer to an object of a class , a pointer to a structure, a point to int, to char … to anything actually. A major difference between a pointer and references is that references are managed, wether in java or .net or any other dynamic language.

A managed environment means that your JVM or .net CLR, takes care of a lot of things for you…

One of the most important of these things are pointers. To be specific, java has no pointers at all, just references. c# does support pointers.

Anyway, in c and c++ you can use a pointer to point to anything simply as follows:


int* p ;
char* p;

this is the declaration, it doesn’t point to anything now… in other languages, if you tried to use this pointer it will throw an exception , in c it wont it will simply get you the value that originally existed in the place you are pointing to… and if you attempted to write to it, you will overwrite someone else’s data or in best cases you will write in a random place where no one knows if it is used or not.

It's IMPORTANT and considered a best practice to initialize your pointers (Use NULL if you don't yet know what they will point to). Pointers are randomly initialized. If you try to access it without initializing, you can get a SEGV when you read from it and/or write to it. Depending on the random "value" it points to.

pointers are used for multiple reasons. I can't remember all of them:

  • C has no "pass by reference". You actually pass the memory address by value here (To change the value of a pointer, you need to pass a pointer to a pointer).
  • Pass by value can be bad if you have a large data structure!
  • Some data structures can only be passed as pointers.
  • In C, you can't practically do strings without pointers.
  • No dynamic memory allocation without pointers.
  • The array declaaration is a pointer to the 1st element of the array!
  • Pointers to functions are important also!

You can get the address of any variable in c using the & operator:


int  x = 0;
int* p = &x;

that way, p is pointer that points to x. to get the value in x we use the * operator:


*p  = 6; 

You could a pointer value to another with no problems:


nt* p2 = p; 

an important usage of the pointer is passing a variable by reference if we have a function as follows:


void  SomeFunctionName (int s)
{
	S = 5;
} 

And used as follows:


int x = 2;
SomeFunctionName(x);

Actually, this didn’t do anything… x will still be 2, because it is called by value, that means it sent a copy of its value to s..

It is important to note that c does not have have out-of-the-box pass by reference.

Written like this:


void  SomeFunctionName (int* s)
{
	*S = 5;
}

And used as follow:


int x = 2;
SomeFunctionName (&x);

In c an array is a pointer to the first element of the array. The point here is the dynamic allocation... Note: add the lib where the function malloc exist in malloc.h and in the code:


int* ar = (int*)malloc (10 * sizeof(int));

note: that in c you can say the following:


ar++;

What if I want to pass an array to a function I would do the following:


void foo (int* par)
{
      par[3] = 3;
}

And pass it using the following:


foo(ar);

If I tried to do that the way in the previous example, like the following:


void foo(int* par)
{
      par = (int*)malloc (10 * sizeof(int));
}

And pass it using the following:


foo(ar);

Nothing will happen, we have changed what the pointer “par” points to not what the original array points to…

For this to work, we need to make a pointer to pointer… That is written like this:


int** pp = &ar;

here we are assigning the address of the ar pointer to pp… that way we can do the following:


*pp  =  (int*)malloc (10 * sizeof(int));

That will change the value of ar… Thus we could do the following to the function:


void foo(int** par)
{
*par = (int*)malloc (10 * sizeof(int));
}

And pass it using the following:

foo(&ar);

That’s all… my goal was to overview the pointers briefly… you will have some long book in the c to get it all but this is the short version… Thank you.

Comments

Hi, Great work, I

Hi,

Great work, I modified it a bit to improve the code formating :)

I had a question though, do we need all the inter-language talk? I find it a bit confusing and distracting.

What do you think ?

MSameer's picture

Pointers are _NOT_

  1. Pointers are _NOT_ references. A pointer is a normal variable but it carries the memory address of another variable. That other variable can be a premitive type (char, int, bool, long, ..) or an aggregate type (Object, structure, array, ...) or a function (pointers to functions).
  2. Java _HAS_NO_POINTERS_. You obtain an object handle but you can't do more.
  3. It's IMPORTANT and considered a best practice to initialize your pointers (Use NULL if you don't yet know what they will point to). Pointers are randomly initialized. If you try to access it without initializing, you can get a SEGV when you read from it and/or write to it. Depending on the random "value" it points to.
  4. pointers are used for multiple reasons. I can't remember all of them:
  • C has no "pass by reference". You actually pass the memory address by value here (To change the value of a pointer, you need to pass a pointer to a pointer).
  • Pass by value can be bad if you have a large data structure!
  • Some data structures can only be passed as pointers.
  • In C, you can't practically do strings without pointers.
  • No dynamic memory allocation without pointers.
  • The array declaaration is a pointer to the 1st element of the array!
  • Pointers to functions are important also!

Thanks for the feedback

Thanks for the feedback, I have made some enhancements with your comment.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.


Dr. Radut | book