// pointers.c
// review of pointers - what will be printed???
#include <stdio.h>
int main (void) {
int count1 = 10;
int *ptr1 = &count1; // showing how to declare a pointer using the *
// also showing how to assign a value to the pointer
// by assigning the ‘address of’ a variable, i.e. &count1
int count2 = 20;
int *ptr2 = &count2;
printf ("ptr1 is %i, ptr2 is %i \n", *ptr1, *ptr2);
ptr1 = ptr2; // showing how to assign one pointer to another; no asterisks or
// anything special is needed
printf ("ptr1 is %i, ptr2 is %i \n", *ptr1, *ptr2);
return 0;
}
OUTPUT:
ptr1 is 10, ptr2 is 20
ptr1 is 20, ptr2 is 20 // after assigning ptr2 to ptr1, they then point to the same thing
// pointerTest.c
// pointers and structs - fill in 2 lines of missing code below
#include <stdio.h>
int main (void)
{
typedef struct aStruct {
int x;
int y;
} myStruct;
myStruct struct1;
struct1.x = 10;
struct1.y = 15;
// declare a pointer called ptr1 that points to the member of struct1 called x
int *aPtr = &(struct1.x); // integer pointer that points to the
// member of struct1 called x
printf ("%i \n", *aPtr);
// declare a pointer called ptr2 that points to the entire struct1 object
myStruct *ptr2 = &(struct1); // myStruct pointer that points to
// the entire struct1 object
printf ("%i \n", (*ptr2).x); // need the parentheses surrounding the
// dereferenced pointer because why??
return 0;
}
OUTPUT:
10
10
// tracing.c
// more practice with pointers - what will be printed???
#include <stdio.h>
int main (void)
{
int values[10] = { -1, 14, -24, 6, 9, 2, -3, 4, 7, 3 };
char word[32] = "The semester is just beginning!";
int i;
int *ptr1 = values;
printf ("ptr1 = %i \n", *ptr1);
int *ptr2 = ptr1 + 3;
printf ("ptr2 = %i \n", *ptr2);
char *ptr3;
ptr3 = word;
printf ("*ptr3 = %c \n", *ptr3);
printf ("*(ptr3 + 4) = %c \n", *(ptr3 + 4));
printf ("Letters: ");
char *ptr4 = word;
for (i=0; i<8; i++) {
printf ("%c", *ptr4);
ptr4 += 4;
}
printf ("\n%s \n", word);
return 0;
}
OUTPUT:
ptr1 = -1
ptr2 = 6
*ptr3 = T
*(ptr3 + 4) = s
Letters: Tss j in
The semester is just beginning!
/* pbr.c pass by reference
what will be printed???
*/
#include <stdio.h>
void modify(int p, int *q, int *r) {
printf ("1. p is %i, q is %i, r is %i \n", p, *q, *r);
p = 27; // passed by value - only the local parameter is modified
*q = 27; // passed by value or reference, check call site to determine which
*r = 27; // passed by value or reference, check call site to determine which
}
int main (void) {
int a = 1;
int b = 1;
int c = 1;
int *x = &c;
modify(a, &b, x);
printf ("2. a is %i, b is %i, c is %i, x is %i \n", a, b, c, *x);
// b and c are changed
return 0;
}
OUTPUT:
1. p is 1, q is 1, r is 1
2. a is 1, b is 27, c is 27, x is 27
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
/* pbv.c pass by value
what will be printed??
*/
#include <stdio.h>
int modify (int a) {
printf ("The address of modify's a is %p \n", &a);
a = 15;
printf ("The value of modify's a is %i \n", a);
}
int main (void) {
int a = 20;
int *ptr = &a;
printf ("The address of main's a is %p \n", &a);
printf ("The address of the value of main's ptr is %p \n", ptr);
modify (a);
printf ("The value of main's a is %i \n", a);
return 0;
}
OUTPUT:
The address of main's a is 0x7fff0782719c
The address of the value of main's ptr is 0x7fff0782719c
The address of modify's a is 0x7fff0782717c
The value of modify's a is 15
The value of main's a is 20
/* static.c what will be the output???
more heap vs stack memory
*/
#include <stdio.h>
int tryToModify (int a) {
static int b = 5;
a = a+5;
b = b+5;
printf ("a is %i, b is %i \n", a, b);
}
int main (void) {
int x = 20;
tryToModify (x);
tryToModify (x);
tryToModify (x);
return 0;
}
OUTPUT:
a is 25, b is 10
a is 25, b is 15
a is 25, b is 20
You can see how a starts back over each time in the tryToModify function and ends up being the same value each time. It is re-created each time that function is called (“pushed onto” the stack) and goes away every time the function ends (“popped off” the stack).
The variable b, however, retains its value every time the function is called. Because it is declared as a static variable, it is stored in the “heap” instead of on the “stack”, and therefore, exists beyond that function and all the way until the program ends. So each time the function is called, b still exists and starts out with the value it had the last time it was modified in that function.
// heap.c
#include <stdio.h>
int main (void)
{
int y;
int *ptr;
static int a; // static variable so you can see that its address in memory is
// very different from the addresses of the other two variables
ptr = &y;
*ptr = 99;
printf ("y is %i \nptr is %p \naddress of ptr is %p \n", y, ptr, &ptr);
ptr = &a;
printf ("the address of a is %p \n", ptr);
return 0;
}
OUTPUT:
y is 99
ptr is 0x7fffaf51065c
address of ptr is 0x7fffaf510650
the address of a is 0x601030
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// malloc.c heap vs stack memory
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int a = 10;
int b = 12;
int *base = NULL;
base = (int *) malloc(sizeof(int) * 100);
printf ("a is %p \n", &a);
printf ("b is %p \n", &b);
printf ("base is %p \n", base);
return 0;
}
OUTPUT:
a is 0x7fffbfb22578
b is 0x7fffbfb2257c
base is 0x1555010 // address is very different; obviously not near the other two in memory