CSE321Week6 - same type • int **x ; // x points to an...

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

View Full Document Right Arrow Icon
CSE 321 Panya Chanawangsa [email protected]
Background image of page 1

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

View Full DocumentRight Arrow Icon
Today’s Agenda • Basic pointer concepts • Pointer declaration • Reference/Dereference Operators • Function pointers • Pointer vs. Array • malloc, calloc, realloc
Background image of page 2
Computer Memory • Think of computer memory as cells • Each cell stores one byte • Use a variable to store data to eliminate the need to know the exact memory location • Use sizeof() to determine the number of bytes a data type occupires
Background image of page 3

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

View Full DocumentRight Arrow Icon
Reference Operator (&) • But if you really want to know the address, int x = 321; printf(“%p”, &x); reads “the address of variable x”
Background image of page 4
• Used to declare a pointer variable int *x; • Stores a memory location printf(“%p”, x); • To get the content of the memory location a pointer variable is pointing to: *x = 321; Dereference Operator (*)
Background image of page 5

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

View Full DocumentRight Arrow Icon
int a[4]; int *x; x = a; *x = 10; // same as a[0] = 10; x = x + 1; *x = 20; // same as a[1] = 20; Pointers vs. Arrays
Background image of page 6
• A pointer that points to another pointer of the
Background image of page 7

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

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

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

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

Unformatted text preview: same type • int **x ; // x points to an integer pointer Pointer to Pointer • Pointers that point to functions return_type (*pointer_name)(param_data_type) void (*p)(int) p points to a void function that has one parameter of type int . Function Pointers char *foo = “Hello, world!”; • But foo only points to a read-only memory! • Your program will crash if you do the following: char *bar = “Random string”; strcpy(foo, bar); Pointers vs. Strings char s1 = “Hello, world”; char s2[100] = “Hello, world”; The first function call allocates just enough memory to hold that string (13 in this case). The second function call allocates exactly 100 bytes. Static Memory Allocation char *s = (char *)malloc(numElements * sizeof(char)); int*i = (int *)calloc(numElements, sizeof(int)); calloc initializes all the values to 0 while malloc does not. http://linux.die.net/man/3/malloc Dynamic Memory Allocation...
View Full Document

This document was uploaded on 10/27/2011 for the course CSE 321 at SUNY Buffalo.

Page1 / 11

CSE321Week6 - same type • int **x ; // x points to an...

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

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