NO

Author Topic: Pointer tutorial example with code included  (Read 3689 times)

post from old forum

  • Guest
Pointer tutorial example with code included
« on: September 13, 2004, 09:52:31 PM »
Hi all
Here is a preliminary Pointer example tutorial. Errors or omissions...please let me know and I will repost it. Hope this helps somebody.

Rom

Code: [Select]

/*Basic Pointer1 Example*/
/*Getting and printing values of a pointer variable*/

/*Include header declarations*/
#include "stdio.h"
#include "stdlib.h"

/*Declare but not define int variables a,b*/
int a, b;

/*Declare but not define int ptr(i.e. address) variable*/
/*also known as a pointer variable */
int *ptr;

/*entry point into program*/
int main(void)
{
/* Variables a and b have a left side values as well as a right side values. */
/* Their left side value are their address values or where they are stored in memory. */
/* Their right hand values are what´s stored at their respective left side addresses */
/* int leftVariableAddressValue = rightValue ie. a = 1, b = 2 */
a = 1;
b = 2;
/*Make ptr to point to the address(i.e left side value) of variable b*/
/*Thus, variable b itself also contains a right side integer value, which, is 2 */
/*Both the left address values as well as the right side values */
/*of a pointer variable can be retreived and manipulated as shown further below*/
ptr = &b;

/*Print to end of line and do carriage return to beginning of next line*/
printf("\n");
/*Address value of a variable can be printed by preceding it with the unary & (i.e.%p.... &a)) */
printf("a's right side value is %d and is stored at left side value(i.e. address)%p\n",a,&a);
printf("b's right side value is %d and is stored at left side value(i.e. address)%p\n",b,&b);

/*To print the address of what a pointer variable is pointing to, it is refered to it by it's vriable name(i.e%p... ptr)*/
/*To print the actual address value of where a pointer variable is itself stored, it´s var is preceded with the unary& (i.e %p... &ptr)*/
printf("ptr's address value of var b is %p and ptr is stored at address%p\n",ptr,&ptr);
printf("\n\n");

/*To print the right side value of what a pointer points to, just precede with *(i.e%d... *ptr)*/
printf("The value of the integer pointed to by ptr (i.e var b), is %d\n",*ptr);
printf("\n\n");

/*De-reference ptr variable and give it another right side value*/
*ptr = 3;
printf("The value of the integer pointed to by ptr (i.e var b), is now %d\n",*ptr);

/* Thus: A pointer Variable is a variable that holds an address of another variable */
/* Thus: Declare a pointer var like so: int *ptr; */
/* Thus: To make it to point to the address of another variable, like so: ptr = &b; */
/* Thus: To de-reference a pointer variable and give it another right side value, like so: *ptr = 3 */
/* Thus: To retreive the right side value of the variable to which it is pointing to, like so: *ptr */
/* Thus: To print the address of what it's pointing to, like so: %p ptr; */
/* Thus: To print the address of the pointer itself, like so: %p &ptr; */
/* Thus: To print the right side value of the variable to which the pointer is pointing to, like so: %d *ptr; */

printf("\n\n");
/* Stdlib pause function which pauses app. and waits for user input to continue */
system("PAUSE");

return 0;
}


ROM

ROM

  • Guest
Second Pointer example
« Reply #1 on: September 17, 2004, 12:56:49 AM »
Code: [Select]
/*Basic Pointer2 Example
Printing right side array subscript values using array notation
as well as pointer derefrencing techniques */

/*Include header declarations */
#include "stdio.h"
#include "stdlib.h"


/*Declare and Define(give values) to first and second arrays */
int first_array[] = {1,2,3,4,5,6};
int second_array[] = {7,8,9,10,11,12};
/*declare but not define first and second pointers */
int *first_ptr;
int *second_ptr;

/*program entry point*/
int main(void)
{
    int counter;
/* point our first pointer to the first array using unary "&" prfix and subscript "[]" postfix */
    first_ptr = &first_array[0];
 
 /* point our second pointer to the second array using no unary prefix or subscript postfix */
 /*which, can be done becuase the name of an array "is" a constant pointer itself. */
 /*Meaning, that while this is ok " second_pointer = second_array",  */
 /*this is'nt ok "second_array = second_pointer".*/
    second_ptr = second_array;
       
    printf("\n\n");
    for(counter = 0; counter < 6; counter++)
    {
 /* This will print first_array's right side values using array notation starting at subscript 0 */      
      printf("first_array[%d] = %d   ",counter, first_array[counter]);
 /* print right side values of first_array using pointer derefrencing, starting at ptr at subscript [0](i,e 1) + counter at 0*/        
      printf("first_ptr + %d = %d\n",counter, *(first_ptr + counter));      
    }

  printf("\n\n");
  for(counter = 0; counter < 6; ++counter)
    {
 /* This will print second_array's right side values using array notation starting at subscript 0 */      
      printf("second_array[%d] = %d   ",counter, second_array[counter]);
 /* print right side values of second_array using pointer derefrencing, starting at ptr at subscript [0](i,e 7) + counter at 0 */        
     printf("second_ptr + %d = %d\n",counter, *(second_ptr + counter));
    }
       
/*Print two empty lines */    
    printf("\n\n");
 /* Stdlib pause function  which pauses app. and waits for user input to continue */          
    system("PAUSE");
    return 0;
}