ECE 15 Pointers
int userInt=10; int* myPtr = NULL; printf("%d\n", myPtr); printf("%p\n", myPtr); printf("%p\n", *myPtr); printf("%d\n", *myPtr); myPtr= &userInt; printf("%d\n", myPtr); printf("%p\n", myPtr); printf("%p\n", *myPtr); printf("%d\n", *myPtr); *myPtr = 20; printf("%d\n", *myPtr); printf("%d", userInt); return 0 ;
0 0 Segmentation Fault Segmentation Fault Address of userInt in decimal Address of userInt in hex 10 0xa 20 20
Call a function MyFct that returns void, with four parameters, the last two being pointers. Call the function with argument variables a, b, c, and d, in that order, all being of type int.
MyFct(a,b, &c, &d);
#include <stdio.h> void SplitIntoTensOnes(int* tensDigit, int* onesDigit, int DecVal){ *tensDigit = (DecVal / 10) % 10; *onesDigit = DecVal % 10; } int main(void) { int tensPlace; int onesPlace; int userInt; userInt = 41; //Solution printf("tensPlace = %d, onesPlace = %d\n", tensPlace, onesPlace); return 0; } Write a function call with arguments tensPlace, onesPlace, and userInt. Be sure to pass the first two arguments as pointers. Sample output for the given program: tensPlace = 4, onesPlace = 1
SplitIntoTensOnes(&tensPlace,&onesPlace,userInt);
What does printf("%p", (void*) &numStudents) output?
The address location of numStudents
#include <stdio.h> //Solution int main(void) { int xValNew; int yValNew; CoordTransform(3, 4, &xValNew, &yValNew); printf("(3, 4) becomes (%d, %d)\n", xValNew, yValNew); return 0; } Define a function CoordTransform() that transforms its first two input parameters xVal and yVal into two output parameters xValNew and yValNew. The function returns void. The transformation is new = (old + 1) * 2. Ex: If xVal = 3 and yVal = 4, then xValNew is 8 and yValNew is 10.
int CoordTransform(int xVal, int yVal, int *xValNew, int (p)yValNew){ (p)xValNew=(xVal+1)*2; (p)yValNew=(yVal+1)*2; }
Provide the code needed to obtain a pointer to a variable named tensDigit
&tensDigit
int i = 3, j = 5, *p = &i, *q = &j, *r; double x; What do the following evaluate to? p == &*p **&p r = &x 7**p/*q+7 * (r = &j) *= * p
1 3 error 11 15
Say you have the following variables int userInt=8; int* myPtr = 3; 1) What does the following output? int userInt=10; int* myPtr = 3; printf("%d\n", userInt); printf("%d\n", myPtr); printf("%p\n", myPtr); printf("%p\n", *myPtr); printf("%d\n", *myPtr);
8 3 0x3 (hex for 3)
Complete the function declaration for MyFct with input parameters w and x, and "output" parameters y and z, in that order, all dealing with type int. Declare pointers by substituting the asterisk for (p) since the asterisk is a bold function in quizlet
void MyFct(int w,int x, int (p)y, int (p)z);
What does myPtr = &numStudents; do? myPtr is pointer
Sets myPtr to fetch whatever value is stored at the address of numStudents. For example, if numStudents = 12 and is stored at location 99, myPtr will be set to 99.
Pointer
Variable that points to another, updates data based on location of variable, immune to end of function discarding.