Ok we're going to dive deep into pointers, starting with how they apply to strings.
But we're also going to explore pointers to any data types, which will be used in the upcoming tutorials.
A pointer is a variable which points to a specified memory address. It doesn't have a value itself, just the address it points to.
The data at that address is interpreted as the same type as the pointer you declared. To declare a pointer you use the * (asteriks) symbol.
//this declares that pMyChar is
//going to point to a memory address,
//and that memory address should be
//interpreted as a character
char *pMyChar;
//this declares that pMyInt is
//going to point to a memory address,
//and that memory address should be
//interpreted as an integer
int *pMyInt;
//this declares that pMyFloat is
//going to point to a memory address,
//and that memory address should be
//interpreted as a float
float *pMyFloat;
so lets look at the usage. Lets say we have
char name[16] = "TrapZZ";
char *pCharacter = name;
the above line tells the pCharacter to point to the first memory address in name
(because the brackets aren't used, it points to the first memory address that name references).
now we're going to introduce a new operator, &. the & (ampersand) means the "address of".
And that is how is should be referred to when we talk :) pCharacter points to a memory address.
If we use name[0] anywhere, that is giving us the value at name[0], which would be 'T'. We can't have
pCharacter point to a value, it has to point to a memory address. So the way we do it is by using the
"address of" operator. We do
pCharacter = &name[0];
that means pCharacter should point to the address of name[0].
If we look at pCharacter, the value in it would be the memory address (in RAM)
that it points to. RAM address are referred to in hexidecimal numbers. First thing
I want you to do is print out the address pCharacter points to. You can do this with printf
printf( "address pCharacter points to is: %x", pCharacter);
the above statement will give a warning, but not an error. Don't worry about
the warning for now. The %x means print a hexidecimal number. You will get
some memory address like 12ec4. That is the address in ram that pCharacter
is pointing to, pretty cool huh?
Now you want to get the value that is in that address. The way you do it is
using the * operator. It says, get the value in the address that this pointer points to.
printf( "value in address %x is %c", pCharacter, *pCharacter );
__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main( void )
{
int i = 0;
char name[ 16 ];
char *pCharacter = &name[ i ];
printf( "\n Enter your first name :" );
scanf( "%s", name );
for ( i = 0; pCharacter[ i ] != 0; i++ )
{
printf( "\n Value in address %x is ", pCharacter + i );
printf( "%c", pCharacter[ i ] );
}
printf( "\n\n %s\n", pCharacter );
getch( );
}
__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <conio.h>
#include <string.h>
//our own function using a pointer!
int StringLength( char *pString )
{
int i;
for ( i = 0; pString[ i ] != 0; i++ )
{
}
return i;
}
void main(void)
{
int length;
char name[ 32 ];
printf( "\n Enter your name : " );
scanf( "%s", name );
length = StringLength( name );
printf( "\n %s has %d characters.\n", name, length );
getch();
}