Skip to Content

Revision of Pointers, the short version! from Thu, 13/11/2008 - 12:21pm

The revisions let you track differences between multiple versions of a post.

Pointers: A pointer is what is called in other languages “reference”. 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 J VM or .net CLR, takes care of a lot of things for you… One of the most important of these things are pointers. Most languages consider value types and reference types, others like ruby (I think), consider everything to be object and all variables are references… In java and .net, you cannot normally reference an integer variable or a char or any other value type… (actually, in c# you can using unsafe code as .net allows pointer arithmetic, but that is not the point here and it is not a normally used technique anyway) 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. 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; If you wrote (p = 6) you are changing the place p is pointing to … You could a pointer value to another with no problems: int* 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.. In c# you could use the ref keyword or out keyword , here we use a pointer as the parameter and the argument (parameter value) sent is the address of the variable: Written like this: void SomeFunctionName (int* s) {
    • S = 5; } And used as follow: int x = 2; SomeFunctionName (&x); That way, the value of x will actually change. Another very important aspect of pointer are arrays: In c an array is a pointer to the first element of the array. The point here is the dynamic allocation... What I am trying to do is to dynamically allocate an array of elements where the size is only know in runtime… Say we want to reserve an array of int which size is 10. We use the following: 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++; this has changed the ar pointer to point to the second element of the array… that means that is equivalent to: *(ar + 2) That means that you are taking the value of the position in the array 2 blocks away which is the third element in the array. What if I want to pass an array to a function I would do the following: Void foo (int* par) { } And pass it using the following: Foo (ar); That is fine in the previous example if what I am doing is changing is the array content values… but what if I want to change what the array points to , or may be reallocate the array? 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 will reallocate the original array to a new array. 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