7_pdfsam_cs2022 - Takes value in location x (... =)...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Recall: C is call by value Modifying the value of a parameter in a function does not modify variables in the calling function. Recall: C is call by value void foo(int x) { x = 5; } int main(void) { int y = 10; foo(y); printf(“%d\n”, y); // prints 10, not 5 return 0; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Call by value Example void swap ( int a, int b ) { int temp = a; a = b; b = temp; } void main() { int A = 1, B = 2; swap(A, B); printf( "%d %d\n”, A, B ); }
Background image of page 2
Memory Variables are stored in memory Think of memory as a simple array Every location in memory has an address An address is an integer, just like an array index In C, a memory address is called a pointer C lets you access memory locations directly ... 1 3 2 4 5 MAX
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Rules for pointers int *x,y; Declaration *x = . .. ; Assigns value in location x (... = ) *x;
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Takes value in location x (... =) &y; Take address Declaring pointer variables A pointer variable stores a memory address Use a * to declare a pointer variable e.g.: int *p1; // declare a variable called p1 that // stores a pointer to an integer char *p2; // declare a variable called p2 that stores // a pointer to a character Using Pointers C has two unary operators related to pointers & (address of) operator Returns the address of its argument Said another way: returns a pointer to its argument The argument must be a variable name * (dereference) operator Returns the value stored at a given memory address The argument must be a pointer...
View Full Document

Page1 / 6

7_pdfsam_cs2022 - Takes value in location x (... =)...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online