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
/*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