Programação, Aula 04 – 2024/2025
Practical Work nr. 5 – Pointers
Subjects
● Pointers
Exercises
1. The following program shows the basic declaration of pointer. Implement it in your editor,
and explain the results:
#include <stdio.h>
int main(void)
{
int m = 10, n, o;
int *z = &m; // Declaring an integer pointer z and assigning the
address of m to it
// Printing basic information about pointers and variables
printf("\n\n Pointer : Show the basic declaration of pointer :\n");
printf("-------------------------------------------------------\n");
printf(" Here is m=10, n and o are two integer variable and *z is an
integer");
printf("\n\n z stores the address of m = %p\n", z); // Printing the
address stored in z using %p
printf("\n *z stores the value of m = %i\n", *z); // Printing the
value pointed to by z using *z
printf("\n &m is the address of m = %p\n", &m); // Printing the
address of m using &m
printf("\n &n stores the address of n = %p\n", &n); // Printing the
address of n using &n
printf("\n &o stores the address of o = %p\n", &o); // Printing the
address of o using &o
printf("\n &z stores the address of z = %p\n\n", &z); // Printing the
address of z using &z
}
1
Programação, Aula 04 – 2024/2025
2. Write a program that defines a simple double variable and a pointer that points to that
variable. Print the variable’s value by dereferencing a pointer. Then, change the variable’s
value by dereferencing a pointer.
3. Write a program in C that finds the maximum number between two numbers, but using
pointers.
4. Write a program that defines an array of five integers. Use a pointer to print out the entire
array.
5. The next problem declares an array of integers. Extend that program using pointer
arithmetics to print out the third and fourth array elements (don’t forget that pointer
arithmetics change the pointer).
#include <stdio.h>
int main(void)
{
int arr[] = {10, 20, 30, 40, 50};
// continue here…
}
6. The next program defines an array of four pointers to sentences. Remember that sentences
themselves are arrays of characters. Change the program to print the different sentences
using a cycle.
#include <stdio.h>
int main(void)
{
char *p[] = {"This is the first sentence.",
"This is the second sentence.",
"This is the third sentence.",
"This is the last sentence."};
//Continue here
}
7. The next program asks the user to fill an array of integers (maximum 15 numbers, first ask
the user how many elements are to be filled). Complement the program by printing the
elements of the array in reverse order (don’t forget about pointer arithmetics!).
2
Programação, Aula 04 – 2024/2025