Week 6 Lecture Code
// Pantea Aria
// introduction to pointers
#include <stdio.h>
int main() {
int x = 90;
// what is the address (or reference) of x
printf ("the address of x in memory is %p\n", &x);
printf ("the value of x is %d\n", x);
// I can store the address of x in another variable
// that variable is called a pointer
int *ptr;
// & is called reference/address operator
ptr = &x;
printf("the value of ptr is %p\n", ptr);
// dereference
// having access to where the pointer is pointing to
int y = *ptr; //same as int y = x;
printf ("y is the value of where ptr is pointing to %d\n", y);
// change the value of x indirectly
// directly means e.g x = 100;
*ptr = 100; // same as x = 100; not as y = 100;
printf("the new value of x is %d\n", x);
printf ("y is %d\n", y);
y = *ptr; // y will be 100
return 0;
}
// When we declare a variable in C, where does it live?
// (This leads to the idea of memory locations.)
// What do you think this operator & does in C?
// int x = 10;
// printf("%p\n", &x); // What does this print? shows some hex value
// Can a variable store another variable’s memory address?
// What is the value of this variable, and what is its address?
// int a = 5; value is 5, some hex value &a with %p
// How can we print the address of a? printf ("%p", &a);
// value: printf ("%d", a);
// Can we store this address in another variable? yes, I must declare a pointer
// If we store the address of a in a variable, how can we use it to get the value back?
// int a = 10;
// int *p = &a;
// *p = 20;
// printf("%d\n", a); // What will this print?
// Predict the output and explain:
// int x = 3;
// int y = 4;
// int *p = &x; //declare a pointer to x, p has the address of x, referencing
// *p = y; //deferencing
// printf("%d\n", x);
// What’s the difference between p = &x; and *p = x;?
// What is happening here?
// int a = 100;
// int *ptr1 = &a;
// int *ptr2 = ptr1;
// printf("%d\n", *ptr2);
// Pantea Aria
// playing with pointers
#include <stdio.h>
int main(void) {
int a = 5;
int b = 10;
int *p = &a;
int *q = &b;
printf("Before:\n");
printf("a = %d, b = %d\n", a, b);
// what is happening here?
// let's draw diagrams
*p = *p + *q;
*q = *p - *q;
*p = *p - *q;
//*p = 100 - *q
// *q = *p + *q - 90;
// int y = *p / 10;
printf("After:\n");
printf("a = %d, b = %d\n", a, b); //what will we see???
// printf(" .... ");
return 0;
}
// Pantea Aria
// pointers
// Write a program that:
// Declares and initializes an array of 5 integers (e.g., {1, 2, 3, 4, 5}).
// Uses the name of the array as a pointer and
// doubles the value of each element using pointer arithmetic
// Prints the array before and after doubling the values, using the pointer (not array indexing).
#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5}; // {2, 4, 6, 8, 10}
int i = 0;
while (i < 5) {
//numbers[i] = numbers[i] * 2;
*(numbers + i) = *(numbers + i) * 2;
printf ("%d\n", *(numbers + i));
i++;
}
return 0;
}
// Pantea Aria
// pointers
// Write a program that:
// Defines an integer variable in main.
// uses function square_number to square the value (multiply it by itself)
// Print the result in main.
#include <stdio.h>
void square_number (int *pointer);
int main() {
int number = 100;
printf("number before function is %d\n", number);
// call function square_number
square_number(&number);
printf("number after function is %d\n", number);
return 0;
}
void square_number(int *p) {
*p = *p * (*p);
}
// referncing means, getting the address of a variable
//derefrencing means getting the value stored in an address
// Pantea Aria
// pointers
// Write a program that:
// Reads two integers from the user.
// Passes their addresses to a function called add.
// The function should add the values and return the result;
// Print the result
#include <stdio.h>
int add(int *p, int *q);
int main() {
// Reads two integers from the user.
printf ("Enter two integers:");
int num1, num2;
scanf("%d %d", &num1, &num2);
// Passes their addresses to a function called add.
// add the values and return the result
int result = add(&num1, &num2);
// Print the result
printf("Result is %d\n", result);
return 0;
}
// function definition/body
int add(int *p, int *q) {
int r = *p + *q;
return r;
}
// Pantea Aria
// pointers
// Write a program that:
// Reads two integers from the user.
// write ONE function that adds AND multiplies the values and print the results in main.
// Print the result in main.
#include <stdio.h>
void add_product(int num1, int num2, int *prod_result, int *add_result);
int main() {
// Reads two integers from the user.
printf ("Enter two integers:");
int num1, num2;
scanf("%d %d", &num1, &num2);
//write ONE function that adds AND multiplies
//the values and print the results in main.
int prod_result;
int add_result;
add_product(num1, num2, &prod_result, &add_result);
printf ("Add is %d and product is %d\n", add_result, prod_result);
return 0;
}
void add_product(int n1, int n2, int *p, int *q) {
*p = n1 * n2;
*q = n1 + n2;
return;
}
// Pantea Aria
// pointers
// Write a program that:
// Declares and initializes an array of 5 integers (e.g., {10, 20, 30, 40, 50}) in main.
// Passes the array and its size to a function called half_array.
// Print the array before and after calling the function.
#include <stdio.h>
void half_array(int *numbers, int size);
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
printf("Original array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
// print out the address of array (the name of the array)
// 1. using the name of the array
printf ("The address of array is %p\n", numbers);
// 2. the address of the first element in the array
printf ("The address of array is %p\n", &numbers[0]);
// call function half_array
half_array(numbers, 5);
printf("array after calling function: ");
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
void half_array(int *numbers, int size) {
int i = 0;
while (i < size) {
numbers[i] = numbers[i] / 2;
i++;
}
return;
}
// print out the address of the array from the function too
// Pantea Aria
// pointers and strings
#include <stdio.h>
int main() {
char str[] = "Hello World";
// make a pointer *s to this string
char *s = str;
// char *s = &str[0];
// print out each character
printf("str is %s\n", str);
printf ("str char by char:");
int i = 0;
while(s[i]) {
putchar(s[i]); //putchar(s[i]);
i++;
}
printf("\n");
printf("using the pointer %s", s);
printf("derefrencing char by char:");
i = 0;
while (*(s + i)) {
putchar(*(s + i)); // putchar(*s[i]);
i++;
}
printf("\n");
// do not change str
// print out the address of the string
printf("the address of str is %p\n", str);
printf("The address of str is %p\n", s);
// print out the address of where 'W' is stored
printf ("Address of W is %p and the value is %c\n", &str[6], str[6]);
// can you move s to point to 'W' and not 'H'? how?
s = s + 6;
printf ("s is now %p\n", s);
// Can I move str to point to 'W' and not 'H'??? NO pls don't
return 0;
}
// Pantea Aria
// pointers
// Write a program that:
// Declares a string in main (e.g., "Programming is fun").
// Passes the string to a function called count_vowels.
// The count_vowels function uses a pointer to traverse the string and
// count how many vowels (a, e, i, o, u) are in it.
// main then prints the total number of vowels.
#include <stdio.h>
int count_vowels(char *s); // int count_vowels(char s[]);
int is_vowel(char ch);
int main() {
char sentence[] = "Programming is fun";
int result = count_vowels(sentence);
printf("Number of vowels: %d\n", result);
// your turn
return 0;
}
int count_vowels(char *s) {
int i = 0;
int count = 0;
while(s[i]) {
if (is_vowel(s[i])) {
count++;
}
i++;
}
return count;
}
int is_vowel(char ch) {
return (ch == 'a' ||
ch =='e' ||
ch == 'i' ||
ch == 'o' ||
ch == 'u');
}
// Pantea Aria
// introduction to pointers
#include <stdio.h>
int main() {
int x = 90;
// what is the address (or reference) of x
printf ("the address of x in memory is
printf ("the value of x is
// I can store the address of x in another variable
// that variable is called a pointer
// & is called reference/address operator
// dereference
// change the value of x indirectly
return 0;
}
// When we declare a variable in C, where does it live?
// (This leads to the idea of memory locations.)
// What do you think this operator & does in C?
// int x = 10;
// printf("%p\n", &x); // What does this print? shows some hex value
// Can a variable store another variable’s memory address?
// What is the value of this variable, and what is its address?
// int a = 5; value is 5, some hex value &a with %p
// How can we print the address of a? printf ("%p", &a);
// value: printf ("%d", a);
// Can we store this address in another variable? yes, I must declare a pointer
// If we store the address of a in a variable, how can we use it to get the value back?
// int a = 10;
// int *p = &a;
// *p = 20;
// printf("%d\n", a); // What will this print?
// Predict the output and explain:
// int x = 3;
// int y = 4;
// int *p = &x; //declare a pointer to x, p has the address of x, referencing
// *p = y; //deferencing
// printf("%d\n", x);
// What’s the difference between p = &x; and *p = x;?
// What is happening here?
// int a = 100;
// int *ptr1 = &a;
// int *ptr2 = ptr1;
// printf("%d\n", *ptr2);
// Pantea Aria
// playing with pointers
#include <stdio.h>
int main(void) {
int a = 5;
int b = 10;
int *p = &a;
int *q = &b;
printf("Before:\n");
printf("a = %d, b = %d\n", a, b);
// what is happening here?
// let's draw diagrams
*p = *p + *q;
*q = *p - *q;
*p = *p - *q;
printf("After:\n");
printf("a = %d, b = %d\n", a, b); //what will we see???
return 0;
}
// Pantea Aria
// 20/10/2025
// pointers
// Write a program that:
// Declares and initializes an array of 5 integers (e.g., {1, 2, 3, 4, 5}).
// Uses the name of the array as a pointer and
// doubles the value of each element using pointer arithmetic
// Prints the array before and after doubling the values, using the pointer (not array indexing).
#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5}; // {2, 4, 6, 8, 10}
return 0;
}
// Pantea Aria
// 20/10/2025
// pointers
// Write a program that:
// Defines an integer variable in main.
// uses function square_number to square the value (multiply it by itself)
// Print the result in main.
#include <stdio.h>
void square (int *pointer);
int main() {
int number = 100;
printf("number before function is %d\n", number);
// call function square_number
printf("number after function is %d\n", number);
return 0;
}
// referncing means, getting the address of a variable
//derefrencing means getting the value stored in an address
// Pantea Aria
// pointers
// Write a program that:
// Reads two integers from the user.
// Passes their addresses to a function called add.
// The function should add the values and return the result;
// Print the result
#include <stdio.h>
int main() {
}
// Pantea Aria
// pointers
// Write a program that:
// Reads two integers from the user.
// write ONE function that adds AND multiplies the values and print the results in main.
// Print the result in main.
#include <stdio.h>
int main() {
// Pantea Aria
// pointers
// Write a program that:
// Declares and initializes an array of 5 integers (e.g., {10, 20, 30, 40, 50}) in main.
// Passes the array and its size to a function called half_array.
// Print the array before and after calling the function.
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
printf("Original array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
// print out the address of array (the name of the array)
// print out the address of array (the address of the first element)
// call function half_array
printf("array after calling function: ");
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
// print out the address of the array from the function too
// Pantea Aria
// pointers and strings
#include <stdio.h>
int main() {
char str[] = "Hello World";
// make a pointer *s to this string
// print out each character
// print out the address of the string
// print out the address of the where 'W' is stored
// can you move s to point to 'W' and not 'H'? how?
// Can I move str to point to 'W' and not 'H'???
return 0;
}
// Pantea Aria
// pointers
// Write a program that:
// Declares a string in main (e.g., "Programming is fun").
// Passes the string to a function called count_vowels.
// The count_vowels function uses a pointer to traverse the string and
// count how many vowels (a, e, i, o, u) are in it.
// main then prints the total number of vowels.
#include <stdio.h>
int main() {
char sentence[] = "Programming is fun";
int result = count_vowels(sentence);
printf("Number of vowels: %d\n", result);
// your turn
return 0;
}
// Pantea Aria
// pointers recap
// Write a program that:
// send two variables to a function
// swap them
// print them in the main
#include <stdio.h>
void swap(int *num1, int *num2);
int main() {
int num1, num2;
printf("Enter two integers:");
scanf("%d %d", &num1, &num2);
printf("\nnum1 = %d and num2 = %d before swap", num1, num2);
// call swap
swap(&num1, &num2);
printf("\nnum1 = %d and num2 = %d after swap\n", num1, num2);
return 0;
}
void swap(int *num1, int *num2) {
int temp = *num1;
*num1 = *num2;
*num2 = temp;
printf("\nnum1 = %d and num2 = %d in swap", *num1, *num2);
}