Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS111 Computer Programming

Similar presentations


Presentation on theme: "CS111 Computer Programming"— Presentation transcript:

1 CS111 Computer Programming
Introduction to pointers

2 Idea 308 roomNo Location Name Location Name 4294953952
Location Address Can we see this values?

3 Question: Is it a variable? Or a Constant ?
Definition A pointer is a symbolic representation of an address in the computer's memory. Question: Is it a variable? Or a Constant ? We can change We can't change

4 int *roomNoPtr = &roomNo;
Pointer Variable int *roomNoPtr = &roomNo; roomNo roomNoPtr 308

5 The Jargon of Pointers int a = 10; int *b; b = &a; b has address of a
b is an integer pointer Value at address pointed by b is an int

6 Pointer of other data types
Pointer is assigned to a specific data type also Be careful while casting !!

7 Passing Address to Function
Example of Call by Reference Address of the variables are passed to the function Operation happens on the actual address No need to return

8 The Jargon of Pointers int a = 10; int *b; b = &a; b has address of a
b is an integer pointer Value at address pointed by b is an int

9 Pointer of other data types
Pointer is assigned to a specific data type also Be careful while casting !!

10 Pointers and 1D Array int myIntArr [10]; float myFloatArr [10];
-9 20 35 64 77 -85 74 32 float myFloatArr [10]; 2.5 3.6 7.5 -8.2 0.25 3.11 4.69 8.25 -10.12 3.66 char myCharArr [10]; A C B F c E X y Z O

11 1D Array Memory Allocation
char myCharArr [10]; int myIntArr [10]; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

12 Pointer to an Integer vs Pointer to an Array of Integer
int myIntArr [10]; int *myPtr = &myIntArr[0]; myPtr ++;

13 Pointer to an Integer vs Pointer to an Array of Integer
int myIntArr [10]; int (*myPtr)[10] = &myIntArr; myPtr ++;

14 Passing Array to Function
1. Element by Element 2. Address of individual elements 3. Entire Array/ Base Address of the Array

15 malloc() e.g. char *line; int linelength = 100;
line = (char*)malloc(linelength); malloc() To allocate memory use void *malloc(size_t size); Takes number of bytes to allocate as argument. Use sizeof to determine the size of a type. Returns pointer of type void *. A void pointer may be assigned to any pointer. If no memory available, returns NULL.

16 malloc() example Note we cast the return value to int*.
To allocate space for 100 integers: int *ip; if ((ip = (int*)malloc(100 * sizeof(int))) == NULL){ printf("out of memory\n"); exit(); } Note we cast the return value to int*. Note we also check if the function returns NULL.

17 calloc() Similar to malloc(), the main difference is that the values stored in the allocated memory space are zero by default. With malloc(), the allocated memory could have any value. calloc() requires two arguments - the number of variables you'd like to allocate memory for and the size of each variable. void *calloc(size_t nitem, size_t size); Like malloc(), calloc() will return a void pointer if the memory allocation was successful, else it'll return a NULL pointer.


Download ppt "CS111 Computer Programming"

Similar presentations


Ads by Google