Chp_3 - Pointers
Chp_3 - Pointers
CHAPTER 3.
POINTERS
CENGİZ RİVA
Pointers
• The actual data type of the value of all pointers, whether integer,
float, character, or otherwise, is the same, a long hexadecimal
number that represents a memory address.
• The only difference between pointers of different data types is
the data type of the variable or constant that the pointer points
to.
Pointer Declaration
• Following is an invalid pointer declaration.
#include <stdio.h>
int main ()
{
float x,y;
int *p;
x=1.5;
p=&x;
y=*p;
printf("value of y= %f\n",y);
}
ptr++;
Pointer Arithmetic
ptr++;
• Now, after the above operation, the ptr will point to the location
1004 because each time ptr is incremented, it will point to the
next integer location which is 4 bytes next to the current location.
#include <stdio.h>
void main ()
{
int *ptr,x;
x=10;
ptr=&x;
printf("The value of ptr is : %lu\n", ptr );
ptr++;
printf("The value of ptr after ++ : %lu\n", ptr);
}
Pointer Arithmetic
• If ptr points to a character whose address is 1000, then above
operation will point to the location 1001 because next
character will be available at 1001.
#include <stdio.h>
void main ()
{
char *ptr,x;x='A';
ptr=&x;
printf("The value of ptr is : %u\n", ptr );
ptr++;
printf("The value of ptr after ++ : %u\n", ptr);
}
Pointer Arithmetic
int *p[3];
#include <stdio.h>
void main()
{
int x,*p,**q;
x=10;
p=&x;
q=&p;
printf("value of x = %i\n",**q);
}
Problems with Pointers