I am learning pointers and going through the examples from this Stanford PDF Stanford Pointers Guide
I decided to write some code using their examples to get a better handle on what is happening. I am working from page 21 of the PDF.
When I run the code I get a segmentation fault, so I know that I'm trying to access memory that doesn't belong to me, but I don't understand where I'm going wrong.
Here is the code I have written:
#include <stdio.h>
#include <stdlib.h>
// function prototypes
void C();
void B();
void main(){
int *worthRef;
int num = 42;
worthRef = #
B(*worthRef);
}
// Takes value of interest by reference and adds 2
void C(int* worthRef){
*worthRef = *worthRef + 2;
printf("num = %d", &worthRef);
}
// adds 1 to value of interest then calls C()
void B(int* worthRef){
*worthRef = *worthRef + 1; // add 1 to worthRef as before
C(worthRef); // NOTE: no & required. We already have a pointer to
// the value of interest, so it can be
// passed through directly
}