Lesson three Homework
A pointer is a variable that stores the memory address of a computer. Reading data from the memory pointed to by the pointer is called the value of the pointer. Pointers can point to variable addresses of specific types, such as int,long , and double. The pointer can also be a void type,aNULL Pointer, and an uninitialized pointer.
1)
#include <stdio.h>
int main ()
{
int *ptr; declares an int pointer
int val = 1; declares an int value
PTR = &val; Assigning a reference to an int value for a pointer
int deref = *ptr; value the pointer to print the contents stored in the pointer address
printf ("Deref address =%ld, value =%d\n", PTR, deref);
}
2. Pointers and Arrays
#include <stdio.h>
int main ()
{
int myarray[4] = {1,2,3,0};
int *ptr = myarray;
printf ("PTR address =%ld, value *ptr=%d\n", ptr,*ptr);
ptr++;
printf ("PTR address =%ld, value *ptr=%d\n", ptr,*ptr);
ptr++;
printf ("PTR address =%ld, value *ptr=%d\n", ptr,*ptr);
ptr++;
printf ("PTR address =%ld, value *ptr=%d\n", ptr,*ptr);
}
3)
#include <stdio.h>
struct Person {
int age;
Char *name;
};
int main ()
{
struct person first;
struct person *ptr;
First.age = 21;
Char *fullname = "full name";
First.name = fullname;
PTR = &first;
printf ("age=%d, name=%s\n", First.age, Ptr->name);
}
C Language Pointers