You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Functions: Passing By Value vs Passing by Reference
Motivating Demonstration: swap.c
Pointers in C
inta=10;
doubleb=10.5;
//To declare a pointer variable that can point to an int variable:int*ptrA;
//int * indicates it an int pointer, ptrA is the variable's name//Declare a pointer variable that can point to a double variable:double*ptrB;
//at this point, ptrA and ptrB point to: who knows//WRONG: make ptrA point to 10:ptrA=10;
//Problems: there may not be a memory address "10"// even if there is it probably does not belong to us// likely outcome: segmentation fault//Special memory location called NULLptrA=NULL;
ptrB=NULL;
//initializing pointers to NULL is good practice//later on you can check for NULL:if(ptrA==NULL) {
printf("ERROR: bad memory location\n");
}
//Want: make ptrA point to the variable a//need a way to get the memory location of the variable a// The referencing operator gives this to us:ptrA=&a;
//ampersand, when applied to a normal variable, gives us its memory location//We also want the opposite: that is, given a pointer// variable, we want a way to access what is stored at// that memory location // need: the DEreferncing operator, simply just the asterisk*ptrA=15;
//POinters allow you to define functions that can take// pointer variables as parameters.// you are passing the memory location of the variables, rather than// their values//PITFALLS: //1. You *can* dereference a normal variableintx=10;
*x=20;
//assigns the value 20 to the memory address 10//likely results in a seg fault//2. You *can* reference a pointer variableint*p=NULL;
intx=&p;
//&p gives the address of the pointer (the address of the address) and places it into the variable x//3. You can really screw up and dointx=10;
*(*(*(x))) =20;
&(&(&(x))) =15;
//Summary:// The referencing operator is the ampersand (&)// it takes a NORMAL VARIABLE and turns it into a pointer value// The dereferencing operator is the asterisk (*)// it takes a POINTER VARIABLE and turns it into a normal variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters