Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

c pointers

#include<stdio.h>

/*
	'*' is the dereference operator; it will grab the value at a memory address.
    	it is also used to declare a pointer variable.
    
    '&' is the address-of operator; it will grab the memory address of a variable.
*/

int main(int argc, char *argv[]) {
	int x = 45;			// Declare integer x to be 45.
    int *int_ptr = &x;	// int_ptr now points to the value of x.  Any changes made to the value at int_ptr will also modify x.
  	
  	x = 5;				// Now, the value of x is 5.  This means that the value at int_ptr is now 5.
  	*int_ptr = 2;		// Now, the value at int_ptr is 2.  This means that x is now 0.
  	int_ptr = NULL;		// int_ptr now no longer points to anything.  Make sure you never leave a dangling pointer!
    
    return 0;
}
 
PREVIOUS NEXT
Tagged: #pointers
ADD COMMENT
Topic
Name
9+2 =