By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

pointer

P: 1
i want to full detail of pointer concept.
means how can i access pointer in my programs?
why pointer is always consume 2 bytes? and what is the main concept behind the double pointer?
Sep 23 '06 #1
Share this Question
Share on Google+
2 Replies


Banfa
Expert Mod 5K+
P: 8,916
i want to full detail of pointer concept.
means how can i access pointer in my programs?
why pointer is always consume 2 bytes? and what is the main concept behind the double pointer?
A pointer is a variable that holds the address of a memory location. This may or may not be the address of another variable or a piece of allocated memory. often specify what type of thing it is that the pointer is pointing to, however it is possible to specify a pointer that points at anything.

A pointer is not always 2 bytes. In fact on most of the systems I have used pointers have been 4 bytes, however I have used a system where pointers where either 2 or 4 bytes depending on how they were declared. The size of a pointer is not specified by C it is platform (machine and compiler) dependent.

Pointers are useful anywhere where you wish to pass a reference to an object, they are also integral to using dynamically allocated memory because malloc (or new) returns a pointer to the allocated memory.

If by double pointer you mean a pointer to pointer, this is useful anywhere where you want to reference a pointer.
Sep 23 '06 #2

P: 59
let's say you have the following:
int x=54;
int *ptr = NULL;

ptr = &x; // point to the address in the memory where x is stored (i.e. 1000)
cout << *ptr // This will ouput whatever is at the address (1000): 54

to incrementing a pointer works the same as incrementing an integer, you just have to make sure you're incrementing the address the pointer is pointing to:
ptr++;

you can change whatever point is pointing to without changing the variable x that we have above. We know that ptr is pointing to the address of 54 so to change that we do this:
*ptr = 23;

This'll change x to 23.
Here is my quick IP testing function that's using pointers, just to give you an example. It's a shitty code but it's just to show you how the pointers operate:
Expand|Select|Wrap|Line Numbers
  1.         char* ptr = NULL; // main pointer
  2.         int cnt = 0;  //counting the number of dots
  3.         ptr = tAddr;  // assigning initial IP to the pointer
  4.         char tptr[20];  //storage array for checking numbers between dots
  5.         int i= 0;  // storage array integer
  6.         while (*ptr != '\0') {  // while not the end of the IP
  7.             if ((*ptr != '.') && (cnt < 4)) {  //if pointer doesn't equal ".", must be a num
  8.                 tptr[i] = *ptr;  // store the number in the store array
  9.                 i++;  // go to the next cell in array                
  10.             } else if (*ptr == '.') {  // if pointer is a "."
  11.                 i -= 1; // decrement my i because of the "." occurence
  12.                 if (atoi((char*)(tptr)) <= 255) { //check the range of your number
  13.                     int a=0; 
  14.                     for(a=0;a<=i;a++)  // empty storage array for the next number
  15.                         tptr[a] = NULL;
  16.                     i = 0;  // set i back to 0 so we start at the beginning of our storage array
  17.                 } else { // if our number isn't in range
  18.                     int a=0;
  19.                     for(a=0;a<=i;a++)  // empty the array again
  20.                         tptr[a] = NULL;
  21.                     cout << "not in range"; // ouput error
  22.                     i=0; 
  23.                 }
  24.                 cnt++; // increment dot counter
  25.             }
  26.                 ptr++; // increment the poiner
  27.         }
  28.  
Sep 24 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.