CMPE-013/L. Introduction to C Programming

Size: px
Start display at page:

Download "CMPE-013/L. Introduction to C Programming"

Transcription

1 CMPE-013/L Introduction to C Programming Gabriel Hugh Elkaim Winter 2015 and memory Pointer/array equivalency Pointer arithmetic and the stack and strings Arrays of ointers 1

2 Syntax tye *trname; How to Create a Pointer Variable In the context of a declaration, the *merely indicates that the variable is a ointer tyeis the tye of data the ointer may oint to Pointer usually described as a ointer to tye int *iptr; // Create a ointer to int int *iptr, x; // Create a ointer to int and an int float *fptr1, *fptr2; // Create 2 float ointers Initialization To set a ointer to oint to another variable, we use the &oerator (address of), and the ointer variable is used withoutthe dereference oerator *: = &x; This assigns the address of the variable xto the ointer (now oints to x) Note: must be declared to oint to the tye of x(e.g. int x; int *;) 2

3 Dereferencing When accessing the data ointed to by a ointer, we use the ointer withthe dereference oerator *: y = *; This assigns to the variable y, the value of what is ointing to (xfrom the last slide) Using *, is the same as using the variable it oints to (e.g. x) Dereferencing examle int x = 6, *; // int and a ointer to int = &x; // Assign the address of x * = 5; // Same as x = 5; &xis a constant memory value It reresents the address of x The address of xwill never change is a variable ointer to int It can be assigned the address of any int It may be assigned a new address any time 3

4 Dereferencing examle int x = 6, *; // int and a ointer to int = &x; // Assign the address of x * = 5; // Same as x = 5; *reresents the data ointed to by *may be used anywhere you would use x *is the dereference oerator, also called the indirection oerator In the ointer declaration, the only significance of *is to indicate that the variable is a ointer rather than an ordinary variable Another view Contents of the Mailbox (variable x) of Mailbox (&x) 105 Bank of Mailboxes (memory locations) 4

5 Another view Contents of the Mailbox (x, *) of Mailbox (&x, ) = &x; 105 Bank of Mailboxes (memory locations) Another view Contents of the Mailbox (x, *) of Mailbox (&x, ) * = 2; 105 Bank of Mailboxes (memory locations) 5

6 Dereferencing non-rimitives Comlex x = 0.6, 1.2, *; = &x; ->re = 5; ->rereresents the data ointed to by ->remay be used anywhere you would use x.re ->is the structure dereference oerator, equivalent to (*).re In the ointer declaration, the only significance of *is to indicate that the variable is a ointer rather than an ordinary variable Dereferencing non-rimitives void MyFunc(Comlex *x) Comlex t = *x; x->re /= t.re * t.re + t.im * t.im; x->im /= t.re * t.re + t.im * t.im; 6

7 Dereferencing non-rimitives x Stack (to) t Return address x MyFunc() void MyFunc(Comlex *x) Comlex t = *x; x re im Dereferencing non-rimitives t.re t.im Stack (to) Return address x MyFunc() void MyFunc(Comlex *x) Comlex t = *x; 7

8 How Work int x, y; int *; x = 0xDEAD; y = 0xBEEF; = &x; * = 0x0100; = &y; * = 0x0200; Variable at x y 0x08B8 0x08BC 0x08C0 0x08C4 0x08C8 0x08CC 0x08D0 0x08D4 How Work int x, y; int *; x = 0xDEAD; y = 0xBEEF; = &x; * = 0x0100; = &y; * = 0x0200; Variable at x y 0000 DEAD 0x08B8 0x08BC 0x08C0 0x08C4 0x08C8 0x08CC 0x08D0 0x08D4 8

9 How Work int x, y; int *; x = 0xDEAD; y = 0xBEEF; = &x; * = 0x0100; = &y; * = 0x0200; Variable at x y 0000 DEAD 0000 BEEF 0x08B8 0x08BC 0x08C0 0x08C4 0x08C8 0x08CC 0x08D0 0x08D4 How Work int x, y; int *; x = 0xDEAD; y = 0xBEEF; = &x; * = 0x0100; = &y; * = 0x0200; Variable at x y 0000 DEAD 0000 BEEF BC 0x08B8 0x08BC 0x08C0 0x08C4 0x08C8 0x08CC 0x08D0 0x08D4 9

10 How Work int x, y; int *; x = 0xDEAD; y = 0xBEEF; = &x; * = 0x0100; = &y; * = 0x0200; Variable at x y BEEF BC 0x08B8 0x08BC 0x08C0 0x08C4 0x08C8 0x08CC 0x08D0 0x08D4 How Work int x, y; int *; x = 0xDEAD; y = 0xBEEF; = &x; * = 0x0100; = &y; * = 0x0200; Variable at x y BEEF C0 0x08B8 0x08BC 0x08C0 0x08C4 0x08C8 0x08CC 0x08D0 0x08D4 10

11 int x, y; int *; x = 0xDEAD; y = 0xBEEF; = &x; * = 0x0100; = &y; * = 0x0200; How Work Variable at x y C0 0x08B8 0x08BC 0x08C0 0x08C4 0x08C8 0x08CC 0x08D0 0x08D4 and Arrays A Quick Reminder Array elements occuy consecutive memory locations int x[3] = 1, 2, 3; x[0] x[1] x[2] FFFF FFFF FFFF FFFF 0x07FC 0x0800 0x0804 0x0808 0x080C can rovide an alternate method for accessing array elements 11

12 and Arrays Initializing a Pointer to an Array The array name evaluates to the address of its first (0 th ) element If we declare the following array and ointer variable: int x[5] = 1, 2, 3, 4, 5; int *; We can initialize the ointer to oint to the array using either of these methods: = x; // Works only for arrays = &x[0]; // Same as the above and Arrays A Preview of Pointer Arithmetic Incrementing a ointer will move it to the next element of the array int x[3] = 1, 2, 3; int *; = x; ++; x[0] x[1] x[2] FFFF FFFF FFFF FFFF 0x07FC 0x0800 0x0804 0x0808 0x080C More on this in just a bit 12

13 and Arrays A Preview of Pointer Arithmetic Incrementing a ointer will move it to the next element of the array int x[3] = 1, 2, 3; int *; = x; ++; x[0] x[1] x[2] FFFF FFFF x07FC 0x0800 0x0804 0x0808 0x080C More on this in just a bit and Arrays A Preview of Pointer Arithmetic Incrementing a ointer will move it to the next element of the array int x[3] = 1, 2, 3; int *; = x; ++; x[0] x[1] x[2] FFFF FFFF x07FC 0x0800 0x0804 0x0808 0x080C More on this in just a bit 13

14 Pointer Arithmetic Incrementing Incrementing or decrementing a ointer will add or subtract a multile of the number of bytes of its base tye If we have: float x; float * = &x; ++; We will the address of incremented by 4 since a floatoccuies 4 bytes float *tr; Pointer Arithmetic Incrementing tr = &a; ++tr; Incrementing tr moves it to the next sequential float array element (4 bytes ahead) float a[0] float a[1] float a[2] float a[3] float a[4] float a[5] float a[6] float a[7] float a[8] 0x0050 0x0054 0x0058 0x005C 0x0060 0x0064 0x0068 0x006C 0x0070 0x0074 Words 14

15 Pointer Arithmetic Larger Jums Adding or subtracting any other number with the ointer will change it by a multile of the number of bytes of its tye If we have short int x; short int * = &x; += 3; We will get the address of incremented by 6since a short intvariable occuies 2 bytes of memory float *tr; Pointer Arithmetic Larger Jums tr = a; Adding 6 to tr moves it 6 float array elements ahead (24 bytes ahead) float a[0] float a[1] float a[2] float a[3] float a[4] float a[5] float a[6] float a[7] float a[8] 0x0050 0x0054 0x0058 0x005C 0x0060 0x0064 0x0068 0x006C 0x0070 0x0074 tr += 6; 16-bit Data Memory Words 15

16 Pointer Arithmetic long long x[] = 1, 2, 3; long long * = x; * += 4; ++; * = 0xDEAD1234BEEF; ++; * = 0xF1D04321F00D; -= 2; * = 0xBAD0000F00D1; x[0] x[1] x[2] x07FC 0x0800 0x0804 0x0808 0x080C 0x0810 0x0814 0x0818 Pointer Arithmetic long long x[] = 1, 2, 3; long long * = x; * += 4; ++; * = 0xDEAD1234BEEF; ++; * = 0xF1D04321F00D; -= 2; * = 0xBAD0000F00D1; x[0] x[1] x[2] x07FC 0x0800 0x0804 0x0808 0x080C 0x0810 0x0814 0x

17 Pointer Arithmetic long long x[] = 1, 2, 3; long long * = x; * += 4; ++; * = 0xDEAD1234BEEF; ++; * = 0xF1D04321F00D; -= 2; * = 0xBAD0000F00D1; x[0] x[1] x[2] x07FC 0x0800 0x0804 0x0808 0x080C 0x0810 0x0814 0x0818 Pointer Arithmetic long long x[] = 1, 2, 3; long long * = x; * += 4; ++; * = 0xDEAD1234BEEF; ++; * = 0xF1D04321F00D; -= 2; * = 0xBAD0000F00D1; x[0] x[1] x[2] BEEF 0000 DEAD x07FC 0x0800 0x0804 0x0808 0x080C 0x0810 0x0814 0x

18 Pointer Arithmetic long long x[] = 1, 2, 3; long long * = x; * += 4; ++; * = 0xDEAD1234BEEF; ++; * = 0xF1D04321F00D; -= 2; * = 0xBAD0000F00D1; x[0] x[1] x[2] BEEF 0000 DEAD x07FC 0x0800 0x0804 0x0808 0x080C 0x0810 0x0814 0x0818 Pointer Arithmetic long long x[] = 1, 2, 3; long long * = x; * += 4; ++; * = 0xDEAD1234BEEF; ++; * = 0xF1D04321F00D; -= 2; * = 0xBAD0000F00D1; x[0] x[1] x[2] BEEF 0000 DEAD 4321 F00D 0000 F1D x07FC 0x0800 0x0804 0x0808 0x080C 0x0810 0x0814 0x

19 Pointer Arithmetic long long x[] = 1, 2, 3; long long * = x; * += 4; ++; * = 0xDEAD1234BEEF; ++; * = 0xF1D04321F00D; -= 2; * = 0xBAD0000F00D1; x[0] x[1] x[2] BEEF 0000 DEAD 4321 F00D 0000 F1D x07FE 0x0800 0x0802 0x0804 0x0806 0x0808 0x080A 0x080C Pointer Arithmetic long long x[] = 1, 2, 3; long long * = x; * += 4; ++; * = 0xDEAD1234BEEF; ++; * = 0xF1D04321F00D; -= 2; * = 0xBAD0000F00D1; x[0] x[1] x[2] 000F 00D BAD BEEF 0000 DEAD 4321 F00D 0000 F1D x07FE 0x0800 0x0802 0x0804 0x0806 0x0808 0x080A 0x080C 19

20 Post-Increment/Decrement Syntax Rule Care must be taken with resect to oerator recedence when doing ointer arithmetic: Syntax Oeration Descrition by *++ *(++) (*)++ Post-Increment Pointer Post-Increment data ointed to by Pointer z = *(++); is equivalent to: z = *; = + 1; z = (*)++; is equivalent to: z = *; * = * + 1; Post-Increment / Decrement Syntax int x[3] = 1, 2, 3; int y; int * = x; x[0] y = 5 + *(++); y = 5 + (*)++; Remember: *(++) is the same as *++ x[1] x[2] y x07FC 0x0800 0x0804 0x0808 0x080C 0x0810 0x0814 0x

21 Post-Increment / Decrement Syntax int x[3] = 1, 2, 3; int y; int * = x; x[0] y = 5 + *(++); y = 5 + (*)++; Remember: *(++) is the same as *++ x[1] x[2] y x07FC 0x0800 0x0804 0x0808 0x080C 0x0810 0x0814 0x0818 Post-Increment / Decrement Syntax int x[3] = 1, 2, 3; int y; int * = x; x[0] y = 5 + *(++); y = 5 + (*)++; Remember: *(++) is the same as *++ x[1] x[2] y x07FC 0x0800 0x0804 0x0808 0x080C 0x0810 0x0814 0x

22 Post-Increment / Decrement Syntax int x[3] = 1, 2, 3; int y; int * = x; x[0] y = 5 + *(++); y = 5 + (*)++; Remember: *(++) is the same as *++ x[1] x[2] y x07FC 0x0800 0x0804 0x0808 0x080C 0x0810 0x0814 0x0818 Post-Increment / Decrement Syntax int x[3] = 1, 2, 3; int y; int * = x; x[0] y = 5 + *(++); y = 5 + (*)++; Remember: *(++) is the same as *++ x[1] x[2] y x07FC 0x0800 0x0804 0x0808 0x080C 0x0810 0x0814 0x

23 Pre-Increment/Decrement Syntax Rule Care must be taken with resect to oerator recedence when doing ointer arithmetic: Syntax Oeration Descrition by ++* *(++) ++(*) Pre-Increment Pointer Pre-Increment data ointed to by Pointer z = *(++); is equivalent to: = + 1; z = *; z = ++(*); is equivalent to: * = * + 1; z = *; Pre-Increment / Decrement Syntax int x[3] = 1, 2, 3; int y; int * = x; x[0] y = 5 + *(++); y = (*); Remember: *(++) is the same as *++ x[1] x[2] y x07FC 0x0800 0x0804 0x0808 0x080C 0x0810 0x0814 0x

24 Pre-Increment / Decrement Syntax int x[3] = 1, 2, 3; int y; int * = x; x[0] y = 5 + *(++); y = (*); Remember: *(++) is the same as *++ x[1] x[2] y x07FC 0x0800 0x0804 0x0808 0x080C 0x0810 0x0814 0x0818 Pre-Increment / Decrement Syntax int x[3] = 1, 2, 3; int y; int * = x; x[0] y = 5 + *(++); y = (*); Remember: *(++) is the same as *++ x[1] x[2] y x07FC 0x0800 0x0804 0x0808 0x080C 0x0810 0x0814 0x

25 Pre-Increment / Decrement Syntax int x[3] = 1, 2, 3; int y; int * = x; x[0] y = 5 + *(++); y = (*); Remember: *(++) is the same as *++ x[1] x[2] y x07FC 0x0800 0x0804 0x0808 0x080C 0x0810 0x0814 0x0818 Pre-Increment / Decrement Syntax int x[3] = 1, 2, 3; int y; int * = x; x[0] y = 5 + *(++); y = (*); Remember: *(++) is the same as *++ x[1] x[2] y x07FC 0x0800 0x0804 0x0808 0x080C 0x0810 0x0814 0x

26 The arentheses determine what gets incremented/decremented: Modify the ointer itself Pre- and Post- Increment/Decrement Summary *(++) or *++ and *(++) or *++ Modify the value ointed to by the ointer ++(*) and (*)++ Initialization Ti If a ointer isn't initialized to a secific address when it is created, it is a good idea to initialize it as NULL This will revent it from unintentionally corruting a memory location if it is accidentally used before it is initialized int * = NULL; NULis the character '\0'but NULLis the value of a ointer that oints to nowhere 26

27 and the Stack Beware the stack Memory addresses may not always be valid es referring to the stack have a lifetime tied to that variables scoe Only global, static, and ointers returned by malloc() will always be valid You should almost never use the memory addresses of variables on the stack main() foo() z and the Stack Beware the stack Stack (to) Return address x y b a Return address function int *foo(int x, int y) int z = x + (++y); return &z; caller int *main(void) int a = 6, b = 19; int *c = foo(a, b); rintf("%d\n", *c); 27

28 main() z and the Stack Beware the stack Stack (to) Return address x y b a Return address function int *foo(int x, int y) int z = x + (++y); return &z; caller int main(void) int a = 6, b = 19; int *c = foo(a, b); rintf("%d\n", *c); main() uts() and the Stack Beware the stack Stack (to) Return address s b a Return address function int *foo(int x, int y) int z = x + (++y); return &z; caller int a = 6, b = 19; int *c = foo(a, b); uts("hey!"); rintf("%d\n", *c); 28

29 and Strings So far, we have worked with strings strictly as arrays of char Strings may be created and used with ointers much more elegantly String declaration with a ointer: char *str = "PIC32MX"; str C0 0x08C0 Imlementation varies deending on comiler and architecture used. 3 C I P \0 X M D32 0x91C0 0x91C4 and Strings When initialized, a ointer to a string oints to the first character: char *str = "Microchi"; str M i c r o c h i \0 str += 4 Increment or add an offset to the ointer to access subsequent characters 29

30 and Strings may also be used to access characters via an offset: char *str = "Microchi"; *str == 'M' M i c r o c h i \0 *(str + 4) == 'o' Pointer always oints to "base address" Offsets used to access subsequent chars and Strings Pointer versus Array: Initialization at Declaration Deending on variable tye, art of the variable is constant : Pointer to String Constant char *str = "PIC"; : Character array char str[] = "PIC"; The NULL character '\0' is automatically aended to strings in both cases (array must be large enough). 30

31 and Strings Pointer versus Array: Initialization at Declaration : Pointer Variable char *str1 = "PIC"; char str2[] = "PIC"; str1 str2 RAM 0x9D0008C0 0xA0000FB0 P 0xA0000FB4 I 0xA0000FB5 C 0xA0000FB6 \0 0xA0000FB7 ROM P 0x9D0008C0 I 0x9D0008C1 C 0x9D0008C2 \0 0x9D0008C3 and Strings Pointer versus Array: Assignment in Code An entire string may be assigned to a ointer A character array must be assigned character by character : Pointer Variable char *str; str = "PIC"; : Array Variable char str[4]; str[0] = 'P'; str[1] = 'I'; str[2] = 'C'; str[3] = '\0'; Must exlicitly add NUL character '\0' to array. 31

32 and Strings Comaring Strings If you want to test a string for equivalence, the natural thing to do is: if (str == "Microchi") This is notcorrect, though it might aear to work sometimes This comares the address in strto the address of the string literal "Microchi" The correct way is to use the strcm()function in the standard library which comares strings character by character Arrays of Declaration An array of ointers is an ordinary array variable whose elements haen to all be ointers. char *[4]; This creates an array of 4 ointers to char The array []itself is like any other array The elements of [], such as [1], are ointers to char 32

33 Arrays of Array Elements are Themselves [0] [1] [2] [3] 9D00 3FC0 9D00 3FC3 9D00 3FC7 9D00 3FCC 3FC0 O n \0 3FC3 O f f \0 3FC7 M a i n \0 3FCC A u x \0 Arrays of Initialization A ointer array element may be initialized just like its ordinary variable counterart: [0] = &x; Or, when working with strings: [0] = "My string"; 33

34 char [4][] = "On", "Off", "Main", "Aux" ; Arrays of Different from Two-dimensional Array This creates an two-dimensional array of chars Amount of memory for every string the same Arrays of Array Elements are Sequential [0] [1] [2] [3] E4F F00 614D E FA4-3FA8 O n \0 \0 \0 3FA9-3FAD O f f \0 \0 3FAE-3FB2 M a i n \0 3FB3-3FB7 A u x \0 \0 34

35 Arrays of Dereferencing To use the value ointed to by a ointer array element, just dereference it like you would an ordinary variable: y = *[0]; Using *[0]is the same as using the object it oints to, such as xor the string literal "My String" from the revious slide Arrays of Accessing Strings int i = 0; char *str[] = "Zero", "One", "Two", "Three", "Four", "\0"; int main(void) while(*str[i]!= '\0') rintf("%s\n", str[i++]); while(1); 35

36 Dynamic Memory malloc() free() Dynamic Memory Rationale Memory needs not known at comile time Memory needs to ersist outside of current scoe 36

37 Syntax void *malloc(size_t size); Dynamic Memory malloc() Request memory of sizebytes Usually returned by sizeof oerator Returns valid ointer or NULL tyedef struct float re; float im; Comlex; Comlex *x = malloc(sizeof(comlex)); Dynamic Memory malloc()'d memory tyedef struct float re; float im; Comlex; Comlex *x = malloc(sizeof(comlex)); rintf("comlexre:%f im:%f\n", x->re, x->im); 37

38 tyedef struct float re; float im; Comlex; Dynamic Memory malloc()'d memory Comlex *x = malloc(sizeof(comlex)); x->re = 0.0; x->im = 0.0; rintf("comlexre:%f im:%f\n", x->re, x->im); Dynamic Memory The Hea tyedef struct float re; float im; Comlex; Comlex *x = malloc(sizeof(comlex)); Hea (to) 38

39 Dynamic Memory The Hea tyedef struct float re; float im; Comlex; Comlex *x = malloc(sizeof(comlex)); Comlex *x = malloc(sizeof(comlex)); Comlex *x = malloc(sizeof(comlex)); Comlex *x = malloc(sizeof(comlex)); Comlex *x = malloc(sizeof(comlex)); Hea (to) NULL tyedef struct float re; float im; Comlex; Dynamic Memory NULL ointers Comlex *x = malloc(sizeof(comlex)); x->re = 0.0; x->im = 0.0; rintf("comlexre:%f im:%f\n", x->re, x->im); 39

40 tyedef struct float re; float im; Comlex; Dynamic Memory NULL ointers Comlex *x = malloc(sizeof(comlex)); Comlex y = *x; tyedef struct float re; float im; Comlex; Dynamic Memory NULL ointers Comlex *x = malloc(sizeof(comlex)); if (x) x->re = 0.0; x->im = 0.0; rintf("comlexre:%f im:%f\n", x->re, x->im); 40

41 tyedef struct float re; float im; Comlex; Dynamic Memory NULL ointers Comlex *x = malloc(sizeof(comlex)); if (x) x->re = 0.0; x->im = 0.0; rintf("comlexre:%f im:%f\n", x->re, x->im); Syntax void free(void *tr); Dynamic Memory free() Frees memory ointed to by tr Must have been returned by malloc() tyedef struct float re; float im; Comlex; Comlex *x = malloc(sizeof(comlex)); free(x); 41

42 Dynamic Memory Invalid free()ing // Non-initialized ointers Comlex *x; free(x); // Invalid! // NULL ointers Comlex *y = NULL; free(y); // Invalid! // Non-hea ointers char *z = "Hey!"; free(z); // Invalid! // Hea ointers not returned by malloc() Comlex *w = malloc(sizeof(comlex)); free(&w->re); // Invalid! 42

43 43

44 44

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming 1 CMPE 013/L Gabriel Hugh Elkaim Sring 2013 2 A Variable's versus A Variable's Value In some situations, we will want to work with a variable's address in memor, rather than the value it contains Variable

More information

Pointers CMPE-013/L. Pointers. Pointers What do they do? Pointers What are pointers? Gabriel Hugh Elkaim Winter 2014

Pointers CMPE-013/L. Pointers. Pointers What do they do? Pointers What are pointers? Gabriel Hugh Elkaim Winter 2014 CMPE-013/L A Variable's versus A Variable's Value In some situations, we will want to work with a variable's address in memor, rather than the value it contains Gabriel Hugh Elkaim Winter 2014 Variable

More information

Pointers and Functions Passing Pointers to Functions CMPE-013/L. Pointers and Functions. Pointers and Functions Passing Pointers to Functions

Pointers and Functions Passing Pointers to Functions CMPE-013/L. Pointers and Functions. Pointers and Functions Passing Pointers to Functions CMPE-013/L Gabriel Hugh Elkaim Winter 2014 Passing Pointers to Functions Normally, functions operate on copies of the data passed to them (pass by value) int x = 2, y = 0; Value of variable passed to function

More information

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming 1 2 3 4 CMPE 013/L Pointers and Functions Gabriel Hugh Elkaim Spring 2013 Pointers and Functions Passing Pointers to Functions Normally, functions operate on copies of the data passed to them (pass by

More information

Storage Allocation CSE 143. Pointers, Arrays, and Dynamic Storage Allocation. Pointer Variables. Pointers: Review. Pointers and Types

Storage Allocation CSE 143. Pointers, Arrays, and Dynamic Storage Allocation. Pointer Variables. Pointers: Review. Pointers and Types CSE 143 Pointers, Arrays, and Dynamic Storage Allocation [Chater 4,. 148-157, 172-177] Storage Allocation Storage (memory) is a linear array of cells (bytes) Objects of different tyes often reuire differing

More information

Pointers (1A) Young Won Lim 12/4/17

Pointers (1A) Young Won Lim 12/4/17 Pointers (1A) Coyright (c) 2010-2017 Young W. Lim. Permission is granted to coy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version

More information

Pointers (1A) Young Won Lim 10/23/17

Pointers (1A) Young Won Lim 10/23/17 Pointers (1A) Coyright (c) 2010-2013 Young W. Lim. Permission is granted to coy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version

More information

Pointers (1A) Young Won Lim 10/18/17

Pointers (1A) Young Won Lim 10/18/17 Pointers (1A) Coyright (c) 2010-2013 Young W. Lim. Permission is granted to coy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version

More information

Applications of Pointers (1A) Young Won Lim 2/27/18

Applications of Pointers (1A) Young Won Lim 2/27/18 Alications of (1A) Coyright (c) 2010-2018 Young W. Lim. Permission is granted to coy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later

More information

Applications of Pointers (1A) Young Won Lim 3/14/18

Applications of Pointers (1A) Young Won Lim 3/14/18 (1A) Coyright (c) 2010-2018 Young W. Lim. Permission is granted to coy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version ublished

More information

Applications of Pointers (1A) Young Won Lim 3/21/18

Applications of Pointers (1A) Young Won Lim 3/21/18 (1A) Coyright (c) 2010-2018 Young W. Lim. Permission is granted to coy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version ublished

More information

Applications of Pointers (1A) Young Won Lim 1/5/18

Applications of Pointers (1A) Young Won Lim 1/5/18 Alications of (1A) Coyright (c) 2010-2017 Young W. Lim. Permission is granted to coy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later

More information

Applications of Pointers (1A) Young Won Lim 4/11/18

Applications of Pointers (1A) Young Won Lim 4/11/18 (1A) Coyright (c) 2010-2018 Young W. Lim. Permission is granted to coy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version ublished

More information

14. Memory API. Operating System: Three Easy Pieces

14. Memory API. Operating System: Three Easy Pieces 14. Memory API Oerating System: Three Easy Pieces 1 Memory API: malloc() #include void* malloc(size_t size) Allocate a memory region on the hea. w Argument size_t size : size of the memory block(in

More information

Course organization. Course introduction ( Week 1)

Course organization. Course introduction ( Week 1) Course organization Course introduction ( Week 1) Code editor: Emacs Part I: Introduction to C programming language (Week 2-9) Chapter 1: Overall Introduction (Week 1-3) Chapter 2: Types, operators and

More information

Applications of Pointers (1A) Young Won Lim 4/24/18

Applications of Pointers (1A) Young Won Lim 4/24/18 (1A) Coyright (c) 2010-2018 Young W. Lim. Permission is granted to coy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version ublished

More information

Applications of Pointers (1A) Young Won Lim 3/31/18

Applications of Pointers (1A) Young Won Lim 3/31/18 (1A) Coyright (c) 2010-2018 Young W. Lim. Permission is granted to coy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version ublished

More information

CMPE-013/L. Introduction to C Programming. Unit testing

CMPE-013/L. Introduction to C Programming. Unit testing CMPE-013/L Introduction to C Programming Gabriel Hugh Elkaim Winter 2015 Example Unit testing Testing architecture // Declare test constants testinput some input testexpoutput precalculated output // Calculate

More information

Example: Runtime Memory Allocation: Example: Dynamical Memory Allocation: Some Comments: Allocate and free dynamic memory

Example: Runtime Memory Allocation: Example: Dynamical Memory Allocation: Some Comments: Allocate and free dynamic memory Runtime Memory Allocation: Examle: All external and static variables Global systemcontrol Suose we want to design a rogram for handling student information: tyedef struct { All dynamically allocated variables

More information

Assist. Prof. Dr. Caner ÖZCAN

Assist. Prof. Dr. Caner ÖZCAN Assist. Prof. Dr. Caner ÖZCAN Memory Structure When a variable defined it is stored somewhere in memory. Memory can be thought as block consist of cells. When a variable defined, required number of cell

More information

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming 1 2 3 CMPE 013/L and Strings Gabriel Hugh Elkaim Spring 2013 4 Definition are variables that can store many items of the same type. The individual items known as elements, are stored sequentially and are

More information

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018 CS 31: Intro to Systems Pointers and Memory Kevin Webb Swarthmore College October 2, 2018 Overview How to reference the location of a variable in memory Where variables are placed in memory How to make

More information

Definition. Pointers. Outline. Why pointers? Definition. Memory Organization Overview. by Ziad Kobti. Definition. Pointers enable programmers to:

Definition. Pointers. Outline. Why pointers? Definition. Memory Organization Overview. by Ziad Kobti. Definition. Pointers enable programmers to: Pointers by Ziad Kobti Deinition When you declare a variable o any tye, say: int = ; The system will automatically allocated the required memory sace in a seciic location (tained by the system) to store

More information

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT C Review MaxMSP Developers Workshop Summer 2009 CNMAT C Syntax Program control (loops, branches): Function calls Math: +, -, *, /, ++, -- Variables, types, structures, assignment Pointers and memory (***

More information

Lecture06: Pointers 4/1/2013

Lecture06: Pointers 4/1/2013 Lecture06: Pointers 4/1/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C 1 Pointers A ointer is a variable that contains the (memory) address of another variable What is a memory address?

More information

Linear Data Structure Linked List

Linear Data Structure Linked List . Definition. Reresenting List in C. Imlementing the oerations a. Inserting a node b. Deleting a node c. List Traversal. Linked imlementation of Stack 5. Linked imlementation of Queue 6. Circular List

More information

CS C Primer. Tyler Szepesi. January 16, 2013

CS C Primer. Tyler Szepesi. January 16, 2013 January 16, 2013 Topics 1 Why C? 2 Data Types 3 Memory 4 Files 5 Endianness 6 Resources Why C? C is exteremely flexible and gives control to the programmer Allows users to break rigid rules, which are

More information

type arrayname [ size type arrayname [ size ] = { item ,, item size size

type arrayname [ size type arrayname [ size ] = { item ,, item size size CMPE-3/L and Strings Gabriel Hugh Elkaim Winter 24 Definition are variables that can store many items of the same type. The individual items known as elements, are stored sequentially and are uniquely

More information

Who. Winter Compiler Construction Generic compiler structure. Mailing list and forum. IC compiler. How

Who. Winter Compiler Construction Generic compiler structure. Mailing list and forum. IC compiler. How Winter 2007-2008 Comiler Construction 0368-3133 Mooly Sagiv and Roman Manevich School of Comuter Science Tel-Aviv University Who Roman Manevich Schreiber Oen-sace (basement) Tel: 640-5358 rumster@ost.tau.ac.il

More information

Functions CMPE-013/L. Functions. Functions What is a function? Functions Remember Algebra Class? Gabriel Hugh Elkaim Winter 2014.

Functions CMPE-013/L. Functions. Functions What is a function? Functions Remember Algebra Class? Gabriel Hugh Elkaim Winter 2014. CMPE-013/L Program Structure Gabriel Hugh Elkaim Winter 2014 main()... eat();... drink();... eat()... return; drink()... be_merry(); return; be_merry()... return; Definition What is a function? are self

More information

Storage Class Specifiers Scope and Lifetime of Variables CMPE-013/L. Modules and Scope. Storage Class Specifiers Automatic Variables

Storage Class Specifiers Scope and Lifetime of Variables CMPE-013/L. Modules and Scope. Storage Class Specifiers Automatic Variables CMPE-013/L Modules and Scope Gabriel Hugh Elkaim Winter 2014 Scope and Lifetime of Variables Scope and lifetime of a variable depends on its storage class: Automatic Variables Static Variables External

More information

SYSC 2006 C Winter 2012

SYSC 2006 C Winter 2012 SYSC 2006 C Winter 2012 Pointers and Arrays Copyright D. Bailey, Systems and Computer Engineering, Carleton University updated Sept. 21, 2011, Oct.18, 2011,Oct. 28, 2011, Feb. 25, 2011 Memory Organization

More information

POINTER & REFERENCE VARIABLES

POINTER & REFERENCE VARIABLES Lecture 9 POINTER & REFERENCE VARIABLES Declaring data pointer variables Assignment operations with pointers Referring objects using pointer variables Generic pointers Operations with pointer variables

More information

Pointers, Dynamic Data, and Reference Types

Pointers, Dynamic Data, and Reference Types Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple

More information

In Java we have the keyword null, which is the value of an uninitialized reference type

In Java we have the keyword null, which is the value of an uninitialized reference type + More on Pointers + Null pointers In Java we have the keyword null, which is the value of an uninitialized reference type In C we sometimes use NULL, but its just a macro for the integer 0 Pointers are

More information

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

CprE 288 Introduction to Embedded Systems Exam 1 Review.  1 CprE 288 Introduction to Embedded Systems Exam 1 Review http://class.ece.iastate.edu/cpre288 1 Overview of Today s Lecture Announcements Exam 1 Review http://class.ece.iastate.edu/cpre288 2 Announcements

More information

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

COMP26120: Pointers in C (2018/19) Lucas Cordeiro COMP26120: Pointers in C (2018/19) Lucas Cordeiro lucas.cordeiro@manchester.ac.uk Organisation Lucas Cordeiro (Senior Lecturer, FM Group) lucas.cordeiro@manchester.ac.uk Office: 2.44 Office hours: 10-11

More information

CS 2461: Computer Architecture I

CS 2461: Computer Architecture I Next: Pointers, Arrays, Structs... : Computer Architecture I The real fun stuff in C.. Pointers and Arrays Read Chapters 16, 18 of text Functions, Arrays, Pointers Dynamic data structures Allocating space

More information

[0569] p 0318 garbage

[0569] p 0318 garbage A Pointer is a variable which contains the address of another variable. Declaration syntax: Pointer_type *pointer_name; This declaration will create a pointer of the pointer_name which will point to the

More information

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic Review from Letctures 3 & 4 C++ class syntax, designing classes, classes vs. structs; Passing comparison functions to

More information

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018 Pointer Basics Lecture 13 COP 3014 Spring 2018 March 28, 2018 What is a Pointer? A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Arrays, Pointers and Memory Management

Arrays, Pointers and Memory Management Arrays, Pointers and Memory Management EECS 2031 Summer 2014 Przemyslaw Pawluk May 20, 2014 Answer to the question from last week strct->field Returns the value of field in the structure pointed to by

More information

Memory, Data, & Addressing II CSE 351 Spring

Memory, Data, & Addressing II CSE 351 Spring Memory, Data, & Addressing II CSE 351 Spring 2018 http://xkcd.com/138/ Review Questions 1) If the word size of a machine is 64-bits, which of the following is usually true? (pick all that apply) a) 64

More information

Dynamic memory. EECS 211 Winter 2019

Dynamic memory. EECS 211 Winter 2019 Dynamic memory EECS 211 Winter 2019 2 Initial code setup $ cd eecs211 $ curl $URL211/lec/06dynamic.tgz tar zx $ cd 06dynamic 3 Oops! I made a mistake. In C, the declaration struct circle read_circle();

More information

CPSC213/2014W1 Midterm EXTRA Practice

CPSC213/2014W1 Midterm EXTRA Practice CPSC213/2014W1 Midterm EXTRA Practice DEC/HEX/BIN NUMERACY 1. Convert into decimal: 1a. 0x33 1b. 0x57 1c. 0xaf 1d. 0x7a 1e. 0x1234 1f. 0x69bd 1g. 0x1a64 1h. 0xdead 2. Convert into hex numbers of the specified

More information

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers CS 61C: Great Ideas in Computer Architecture C Arrays, Strings, More Pointers Instructor: Justin Hsia 6/20/2012 Summer 2012 Lecture #3 1 Review of Last Lecture C Basics Variables, Functions, Flow Control,

More information

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21 Department of Computer Science College of Engineering Boise State University September 11, 2017 1/21 Pointers A pointer is a variable that stores the address of another variable. Pointers are similar to

More information

C Arrays and Pointers

C Arrays and Pointers C Arrays and Pointers C Arrays List of contiguous values in memory Array Declaration: Type Name Count int vec[5]; Type: Type of each element Name: Identifier for the entire array Count: Number of elements

More information

CSE351 Autumn 2013 Final Exam (11 Dec 2013)

CSE351 Autumn 2013 Final Exam (11 Dec 2013) CSE351 Autumn 2013 Final Exam (11 Dec 2013) Please read through the entire examination first! We designed this exam so that it can be completed in approximately 90 minutes and, hopefully, this estimate

More information

CS 1613 Lecture 24. Figure 1. Program p01.

CS 1613 Lecture 24. Figure 1. Program p01. Consider a rogram that is required to find all values larger than the average in a list of integers. The list is stored in a file. The rogram must read and store the list to fulfill its requirement. The

More information

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Programming. Pointers, Multi-dimensional Arrays and Memory Management Programming Pointers, Multi-dimensional Arrays and Memory Management Summary } Computer Memory } Pointers } Declaration, assignment, arithmetic and operators } Casting and printing pointers } Relationship

More information

Arrays and Pointers in C. Alan L. Cox

Arrays and Pointers in C. Alan L. Cox Arrays and Pointers in C Alan L. Cox alc@rice.edu Objectives Be able to use arrays, pointers, and strings in C programs Be able to explain the representation of these data types at the machine level, including

More information

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices. Memory and Addresses Memory is just a sequence of byte-sized storage devices. 1 The bytes are assigned numeric addresses, starting with zero, just like the indexing of the cells of an array. It is the

More information

More about BOOLEAN issues

More about BOOLEAN issues More about BOOLEAN issues Every boolean test is an implicit comparison against zero (0). However, zero is not a simple concept. It represents: the integer zero for all integral types the floating point

More information

Fundamentals of Programming Session 20

Fundamentals of Programming Session 20 Fundamentals of Programming Session 20 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology Outlines

More information

Arrays and Memory Management

Arrays and Memory Management Arrays and Memory Management 1 Pointing to Different Size Objects Modern machines are byte-addressable Hardware s memory composed of 8-bit storage cells, each has a unique address A C pointer is just abstracted

More information

by Pearson Education, Inc. All Rights Reserved.

by Pearson Education, Inc. All Rights Reserved. Let s improve the bubble sort program of Fig. 6.15 to use two functions bubblesort and swap. Function bubblesort sorts the array. It calls function swap (line 51) to exchange the array elements array[j]

More information

Understanding Pointers

Understanding Pointers Division of Mathematics and Computer Science Maryville College Pointers and Addresses Memory is organized into a big array. Every data item occupies one or more cells. A pointer stores an address. A pointer

More information

Pointers. 10/5/07 Pointers 1

Pointers. 10/5/07 Pointers 1 Pointers 10/5/07 Pointers 1 10/5/07 Pointers 2 Variables Essentially, the computer's memory is made up of bytes. Each byte has an address, associated with it. 10/5/07 Pointers 3 Variable For example 1:#include

More information

12. Pointers Address-of operator (&)

12. Pointers Address-of operator (&) 12. Pointers In earlier chapters, variables have been explained as locations in the computer's memory which can be accessed by their identifer (their name). This way, the program does not need to care

More information

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016 CS 31: Intro to Systems Pointers and Memory Martin Gagne Swarthmore College February 16, 2016 So we declared a pointer How do we make it point to something? 1. Assign it the address of an existing variable

More information

CS 11 C track: lecture 5

CS 11 C track: lecture 5 CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap Pointers (from last week) Address: location where data stored

More information

Binghamton University. CS-211 Fall Pointers vs. Arrays

Binghamton University. CS-211 Fall Pointers vs. Arrays Pointers vs. Arrays 1 Array Values are Contiguous Right next to each other in memory int vec[6] int m [4][3]; vec[0] vec[1] vec[2] vec[3] vec[4] vec[5] m[0][0] m[0][1] m[0][2] m[1][0] m[1][1] m[1][2] m[2][0]

More information

CMPE-013/L. Introduction to C Programming

CMPE-013/L. Introduction to C Programming CMPE-013/L Introduction to C Programming Bryant Wenborg Mairs Spring 2014 Advanced Language Concepts Unions Function pointers Void pointers Variable-length arguments Program arguments Unions Unions Definition

More information

ECE551 Midterm Version 1

ECE551 Midterm Version 1 Name: ECE551 Midterm Version 1 NetID: There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly. This exam must be individual

More information

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016 CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers Kevin Webb Swarthmore College March 1, 2016 Overview Accessing things via an offset Arrays, Structs, Unions How complex structures are stored

More information

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Lecture 14 No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 1 Outline Static

More information

A flow chart is a graphical or symbolic representation of a process.

A flow chart is a graphical or symbolic representation of a process. Q1. Define Algorithm with example? Answer:- A sequential solution of any program that written in human language, called algorithm. Algorithm is first step of the solution process, after the analysis of

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 9 Pointer Department of Computer Engineering 1/46 Outline Defining and using Pointers

More information

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I C++ ARRAYS POINTERS POINTER ARITHMETIC Problem Solving with Computers-I General model of memory Sequence of adjacent cells Each cell has 1-byte stored in it Each cell has an address (memory location) Memory

More information

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations 55:017, Computers in Engineering C Pointers C Pointers Powerful C feature but challenging to understand Some uses of pointers include Call by reference parameter passage Dynamic data structures Data structures

More information

CSCI 171 Chapter Outlines

CSCI 171 Chapter Outlines Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures

More information

Pointers. Memory. void foo() { }//return

Pointers. Memory. void foo() { }//return Pointers Pointers Every location in memory has a unique number assigned to it called it s address A pointer is a variable that holds a memory address A pointer can be used to store an object or variable

More information

Introduction to C: Pointers

Introduction to C: Pointers Introduction to C: Pointers Nils Moschüring PhD Student (LMU) Nils Moschüring PhD Student (LMU), Introduction to C: Pointers 1 1 Introduction 2 Pointers Basics Useful: Function

More information

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic Announcements: Test 1 Information Test 1 will be held Monday, February 10th, 2014 from 6-7:50pm, Lab sections 1-5 and

More information

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 6: Pointers

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 6: Pointers CS101: Fundamentals of Computer Programming Dr. Tejada stejada@usc.edu www-bcf.usc.edu/~stejada Week 6: Pointers Pointers Pointers are references to other things Pointers are the address of some other

More information

UNIT-I Fundamental Notations

UNIT-I Fundamental Notations UNIT-I Fundamental Notations Introduction to Data Structure We know that data are simply values or set of values and information is the processed data. And actually the concept of data structure is much

More information

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings Lecture 4: Outline I. Pointers A. Accessing data objects using pointers B. Type casting with pointers C. Difference with Java references D. Pointer pitfalls E. Use case II. Arrays A. Representation in

More information

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor Pointer in C Presented By: Pushpendra K. Rajput Assistant Professor 1 Introduction The Pointer is a Variable which holds the Address of the other Variable in same memory. Such as Arrays, structures, and

More information

Chapter 11: Pointers

Chapter 11: Pointers Chapter 11: Pointers Christian Jacob 1 Pointer Basics 3 1.1 What Are Pointers? 3 1.2 Pointer Operators 4 1.3 Pointer Expressions 14 2 Pointers and Arrays 19 2.1 Pointer Arithmetic And Array Indexing 19

More information

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory Pointers A pointer is simply a reference to a variable/object Compilers automatically generate code to store/retrieve variables from memory It is automatically generating internal pointers We don t have

More information

ECE 15B COMPUTER ORGANIZATION

ECE 15B COMPUTER ORGANIZATION ECE 15B COMPUTER ORGANIZATION Lecture 13 Strings, Lists & Stacks Announcements HW #3 Due next Friday, May 15 at 5:00 PM in HFH Project #2 Due May 29 at 5:00 PM Project #3 Assigned next Thursday, May 19

More information

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology Introduction to C++ Massachusetts Institute of Technology ocw.mit.edu 6.096 Pointers 1 Background 1.1 Variables and Memory When you declare a variable, the computer associates the variable name with a

More information

Reminder: compiling & linking

Reminder: compiling & linking Reminder: compiling & linking source file 1 object file 1 source file 2 compilation object file 2 library object file 1 linking (relocation + linking) load file source file N object file N library object

More information

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Bernhard Boser & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic

More information

Final Exam. 11 May 2018, 120 minutes, 26 questions, 100 points

Final Exam. 11 May 2018, 120 minutes, 26 questions, 100 points Name: CS520 Final Exam 11 May 2018, 120 minutes, 26 questions, 100 points The exam is closed book and notes. Please keep all electronic devices turned off and out of reach. Note that a question may require

More information

Pointers and References

Pointers and References Steven Zeil October 2, 2013 Contents 1 References 2 2 Pointers 8 21 Working with Pointers 8 211 Memory and C++ Programs 11 212 Allocating Data 15 22 Pointers Can Be Dangerous 17 3 The Secret World of Pointers

More information

Binghamton University. CS-211 Fall Pointers

Binghamton University. CS-211 Fall Pointers Pointers 1 Memory The act of keeping track of something over time Remembering is the concept of storing information A memory is no good unless you can retrieve that information In a computer, we remember

More information

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor CS 261 Fall 2017 Mike Lam, Professor C Introduction Variables, Memory Model, Pointers, and Debugging The C Language Systems language originally developed for Unix Imperative, compiled language with static

More information

Low-Level C Programming. Memory map Pointers Arrays Structures

Low-Level C Programming. Memory map Pointers Arrays Structures Low-Level C Programming Memory map Pointers Arrays Structures Memory Map 0x7FFF_FFFF Binaries load at 0x20000 by default Stack start set by binary when started Stack grows downwards You will need one stack

More information

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture CS 61C: Great Ideas in Computer Architecture Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c And in Conclusion, 2 Processor Control Datapath Components of a Computer PC Registers Arithmetic

More information

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,

More information

Today s lecture. Pointers/arrays. Stack versus heap allocation CULTURE FACT: IN CODE, IT S NOT CONSIDERED RUDE TO POINT.

Today s lecture. Pointers/arrays. Stack versus heap allocation CULTURE FACT: IN CODE, IT S NOT CONSIDERED RUDE TO POINT. Pointers/arrays Mechanics, syntax Underlying memory model Array indexing == pointer arithmetic As parameters Stack versus heap allocation Stack declaration, scope, lifetime Heap allocation/deallocation

More information

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic

More information

CSCI 2212: Intermediate Programming / C Chapter 15

CSCI 2212: Intermediate Programming / C Chapter 15 ... /34 CSCI 222: Intermediate Programming / C Chapter 5 Alice E. Fischer October 9 and 2, 25 ... 2/34 Outline Integer Representations Binary Integers Integer Types Bit Operations Applying Bit Operations

More information

C++ for Java Programmers

C++ for Java Programmers Basics all Finished! Everything we have covered so far: Lecture 5 Operators Variables Arrays Null Terminated Strings Structs Functions 1 2 45 mins of pure fun Introduction Today: Pointers Pointers Even

More information

last time Assembly part 2 / C part 1 condition codes reminder: quiz

last time Assembly part 2 / C part 1 condition codes reminder: quiz last time Assembly part 2 / C part 1 linking extras: different kinds of relocations addresses versus offset to addresses dynamic linking (briefly) AT&T syntax destination last O(B, I, S) B + I S + O jmp

More information

Introduction to Computer Science Midterm 3 Fall, Points

Introduction to Computer Science Midterm 3 Fall, Points Introduction to Computer Science Fall, 2001 100 Points Notes 1. Tear off this sheet and use it to keep your answers covered at all times. 2. Turn the exam over and write your name next to the staple. Do

More information

CS 61c: Great Ideas in Computer Architecture

CS 61c: Great Ideas in Computer Architecture Arrays, Strings, and Some More Pointers June 24, 2014 Review of Last Lecture C Basics Variables, functioss, control flow, types, structs Only 0 and NULL evaluate to false Pointers hold addresses Address

More information