Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Size: px
Start display at page:

Download "Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8"

Transcription

1 Pointers Variables vs. Pointers: A variable in a program is something with a name and a value that can vary. The way the compiler and linker handles this is that it assigns a specific block of memory within the computer to hold the value of that variable. The size of that block depends on the type of the variable. The way to find the size of the various types on your system is to run the following code that gives the information. #include <iostream> using namespace std; int main(void) { cout <<"size of a short is <<sizeof(short); cout <<"size of a int is" <<sizeof(int); cout << "size of a double is" <<sizeof(double); } Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

2 When we declare a variable, we inform the compiler of two things the name of the variable and the type of the variable. For example, we declare a variable of type integer with the name k by writing: int k; On seeing the "int" part of this statement the compiler: sets aside 4 bytes of memory (on a PC) to hold the value of the integer. It also sets up a symbol table. In that table it adds the symbol k and the relative address in memory where those 4 bytes were set aside. k 1000 Symbol Name Symbol Relative Address k 1000 If, we later write: k = 2;

3 at run time when this statement is executed, the value 2 will be placed in that memory location reserved for the storage of the value of k In C, we refer to a variable such as the integer k as an "object". There are two values associated with the object k: 1. the rvalue: the value of the integer stored there (2 in the above example) 2. the lvalue: the "value" of the memory location, or the address of k. Now consider: int j, k; k = 2; j = 7; line 1 k = j; line 2 In the above, the compiler interprets the j in line 1 as the address of the variable j (its lvalue) and creates code to copy the value 7 to that address. In line 2, however, the j is interpreted as its rvalue. That is, here the j refers to the value stored at the memory location set aside for j, in this case 7.

4 Therefore, the 7 is copied to the address designated by the lvalue of k. In all of these examples, we are using 4 byte integers so all copying of rvalues from one storage location to the other is done by copying 4 bytes. Now, let's say that we have a reason for wanting a variable designed to hold an lvalue (an address). Such a variable is called a pointer variable. Pointers are variables that contain memory addresses as their values. A pointer usually contains an address of a variable that contains a specific value. In C, we define a pointer variable by preceding its name with an asterisk. Example *a, declares a pointer variable called a. In C, we also give our pointer a type that, refers to the type of data stored at the address stored in the pointer. For example int *a, will indicate that the variable pointer a points to a variable of type integer. -A variable usually contains a specific value, we say it directly references a value. -A pointer indirectly references a value, in that it contains the address of the variable, which directly references it.

5 K 7 K directly references the variable whose value is 7 ptr * 7 K Ptr indirectly references a variable whose value is 7. Consider the variable declaration: int *ptr; ptr is the name of the pointer variable (just as k was the name of the integer variable). The '*' informs the compiler that we want a pointer variable, i.e. to set aside however many bytes is required to store an address in memory. The int says that we intend to use our pointer variable to store the address of an integer. When a variable contains the address of another variable, the first variable is said to point to the second, hence the name pointer

6 The general form for declaring a pointer is: data_type *name; data_type: any valid C data type like int, float *: pointer operator name: Any valid C identifier to name the pointer variable Initializing a pointer: Pointers are initialized either when they are declared or in an assignment. A pointer may be initialized to 0, NULL, or an address. -A pointer with the value NULL points to nothing. -NULL is a Symbolic constant defined in the <iostream> header file. -Initializing a pointer to 0 is equivalent to initializing it to NULL, but NULL is preferred. For example: int *ptr; ptr=null; The declaration: ptr = NULL, guarantees that the pointer has become a null pointer.

7 We can test for a null pointer using if (ptr == NULL). Pointer Operators Suppose now that we want to store in ptr the address of the integer variable k. To do this we use the unary & or address operator and write: ptr = &k; & is a unary operator that returns the address of its operand. The & operator retrieves the lvalue (address) of k, and copies that to the rvalue (or content) of the pointer ptr. Now, the value of ptr is 1000 The operand of the address operator & must be a variable. The address operator cannot be applied to constants, to expressions or to variables declared with the storage class register. The second pointer operator, *, is the dereferencing operator, it is the complement of &. * is a unary operator that returns the value of the variable located at the address that follows It is used as follows:

8 *ptr = 7; this will copy 7 to the address pointed to by ptr. Thus if ptr "points to" (contains the address of) k, the above statement will set the value of k to 7. The statement: int t = *ptr; will place the value of k into t. When we use the '*' this way we are referring to the value that ptr is pointing to, not the value of the pointer itself. Similarly, we could write: printf("%d\n",*ptr); to print to the screen the integer value stored at the address pointed to by ptr. #include <iostream> using namespace std; int main(void){ int j, k; int *ptr; j = 1; k = 2; ptr = &k; cout <<"j has the value "<<j <<" and is stored at" << &j <<endl;

9 cout << "k has the value "<< k <<" and is stored at" << &k <<endl; cout << "ptr has the value " << ptr << " and is stored at"<< &ptr <<endl; cout << "The value of the integer pointed to by ptr is" <<*ptr <<endl; return 0; } Sample program output:

10 To review: A variable is declared by giving it a type and a name (e.g. int k;) A pointer variable is declared by giving it a type and a name preceded by the operator * (e.g. int *ptr) where the asterisk tells the compiler that the variable named ptr is a pointer variable and the type tells the compiler what type the pointer is to point to (integer in this case).

11 Once a variable is declared, we can get its address by preceding its name with the unary & operator, as in &k. We can "dereference" a pointer, i.e. refer to the value of that which it points to, by using the unary '*' operator as in *ptr. An "lvalue" of a variable is the value of its address, i.e. where it is stored in memory. The "rvalue" of a variable is the value stored in that variable (at that address). Pointer Expressions: Pointers are valid operands in arithmetic expressions, assignment expressions and comparison expressions. Pointer assignments: One reason we need to identify the type of variable that a pointer points to is that if a variable pointer *ptr points to something, and we have an expression like: *ptr = 2; the compiler will know how many bytes to copy into that memory location pointed to by ptr. There are only two arithmetic operations that we may use on pointers: Addition and subtraction. -A pointer may be incremented (++) or decremented (--) -An integer may be added to a pointer (+, or +=) -An integer may be subtracted from a pointer(- or -=)

12 -or one pointer may be subtracted from another. Pointer Arithmetic: int *ptr; Consider a block in memory consisting of 10 integers in a row. That is, 20 bytes of memory are set aside to hold 10 integers. Now, let's say we point our integer pointer ptr at the first of these integers, located at memory location int1 int2 int3 int4 ptr What happens when we write ptr + 1? Because the compiler "knows" this is a pointer (i.e. its value is an address) and that it points to an integer (its current address, 100, is the address of an integer), it adds (2*1) to ptr instead of 1. So, the pointer "points to" the next integer, at memory location 104. When an integer is added to or subtracted from a pointer, the pointer is not simply incremented or decremented by that integer, but by that integer times the size of the object it points to. So the statement: ptr+=2; will produce 104 (100+ 2*2)

13 Similarly, if ptr had been declared as a pointer to a double, it would add (8*1) to it instead of (2*1). The same goes for other data types such as floats,, short. In C, this operation is referred to as addition using "pointer arithmetic". Similarly, since ++ptr and ptr++ are both equivalent to ptr + 1 incrementing a pointer using the unary ++ operator, either preor post-, increments the address it stores by the amount sizeof(type) where "type" is the data type of the object pointed to. (i.e. 4 for an integer, 2 for a character..). Pointer Comparisons: We can compare two pointers in a relational expression. For instance given two pointers p and q, the following statement is valid: if (p<q) printf( p points to lower memory than q\n ); Generally, pointer comparisons are used when two or more pointers point to a common object. Pointers and Arrays Since a block of 10 integers located contiguously in memory is, by definition, an array of integers, we can use pointer to refer to elements in an array.

14 Consider the following: int my_array[] = {1,23,17,4,-5,100}; Here we have an array containing 6 integers. We refer to each of these integers by means of a subscript to my_array, i.e. using my_array[0] through my_array[5]. Alternatively, we could access them via a pointer as follows: int *ptr; ptr = &my_array[0]; Using either of the statements ++ptr ; or ptr++; increments the pointer to point to the next location or element in the array. Similarly using --ptr or ptr-- decrements the pointer to point to the previous element in the array #include <iostream> using namespace std; int a[] = {1,23,17,4,-5,100}; int *ptr; int main(void) { int i;

15 ptr = &a[0]; /* points to the first element of the array */ cout <<"\n\n"; for (i = 0; i < 6; i++){ cout<< "a["<<i<<"] ="<<a[i]<<endl;/*a*/ cout<<"ptr+"<<i<<"is "; cout <<*(ptr+i)<<endl; /*B */} return 0;} When we run the program, it prints out the same values in either case at lines A and B. Sample program output: a[0] = 1 ptr a[1] = 23 ptr a [2] = 17 ptr Notice how we dereferenced the pointer in line B, i.e. we first added i to it and then dereferenced the new pointer.

16 Also, we could have replaced line B with: cout<<"ptr+ <<i<< = << *ptr++<<endl; and gotten exactly the same results. Operator s precedence: Operators Associativity Type () [] Left to right Highest ! * & (type) * / % + - < <= > >= ==!= &&?: = += -= *= /= %= Right to left Left to right Left to right Left to right Left to right Left to right Left to right Right to left Right to left Unary Binary Binary Relational Relational Logical AND Logical OR Conditional Assignment In C, wherever we might use &var_name[0] we can replace that with var_name, thus in our code where we wrote:

17 ptr = &a[0]; ptr=a we can write: ptr = a; to achieve the same result. This means that the name of the array is the address of first element in the array. Note that while we can write ptr = a; we cannot write a = ptr; The reason is that while ptr is a variable, a is a constant. That is, the location at which the first element of a will be stored and cannot be changed once a[] has been declared. We refer to an array's name as a constant pointer. Since a is a constant, once the compiler establishes where the array itself is to be stored, it "knows" the address of a[0] and on seeing: ptr = a; it simply uses this address as a constant in the code. So the array element a[3] can be referenced with the pointer expression : *(ptr+3) or *(a + 3)

18 The 3 in the expression above is the offset to the pointer. This is called the pointer/offset notation When the pointer points to the beginning of an array, the offset indicates which element of the array is referenced. The offset value is identical to the array subscript or index. The use of ( ) is necessary because * has higher precedence than the +. Pointers can also be indexed or subscripted exactly like arrays, so the expression: ptr[1] (ptr+ 1)refers to a [1]. This is called the pointer/subscript notation. So there are four ways one can use to reference an element in an array: - Array subscripting, - pointer/offset with the array name, - pointer/offset with the pointer name - and pointer subscripting. Calling Functions by Reference: When we pass an integer, for example, to a function, we make a copy of the integer, i.e. get its value and put it on the stack. Within the function, any manipulation of the value passed can in no way effect the original integer.

19 With arrays and pointers, we pass the address of the variable and hence manipulate the values of the original variables. Many functions require the capability to modify one or more variables in the caller, or to pass a pointer to a large data object. When a variable is to be modified by a function, a pointer to that variable is used as an argument and passed to the function. Pointers and the indirection operator are used to simulate a call by reference. #include <iostream> using namespace std; int cubeandsquare(int, int *); int main(void) { int number = 5, cube, square; int *ptr; ptr =&square; cout<< "The original value of number is "<< number<<endl; cube = cubeandsquare(number, ptr); cout<<"the square of number "<< number<<" is "<< *ptr <<endl;

20 cout<<"the cube of number "<< number<< " is "<<cube<<endl; return 0;} int cubeandsquare(int n, int *square) { *square= n * n; return n * n * n; } Sample program Output: The original value of number is 5 The square of number 5 is 25 The cube of number 5 is 125 Another Program: We want to write a program that takes an array of 6 integers and returns the max and the min values in the array #include <iostream> using namespace std; int minmax(int[],int, int*); int a[] = {1,23,17,4,-5,100}; int *ptr, min, max;

21 int main(void){ ptr= &max; min = minmax(a, 6, ptr); cout<<"the max is <<max<< and the min is "<<min<<endl; return 0; } int minmax(int a[], int n, int *max) { int mx, mn, i; mn=a[0]; mx= a[0]; for(i=0; i<n; i++) { } if (mx < a[i]) mx= a[i]; if (mn > a[i]) *max=mx; return (mn); } mn= a[i];

22 Using the const Qualifier with pointers: The const qualifier enables the programmer to inform the compiler that the value of a particular variable should not be modified. If an argument is passed to a function with a call by value, a copy is made of the argument. If the copy is modified in the function, the original value remains unchanged. However, if we use a call by reference, the address in memory is passed, the function called can change the value of the argument. If a value does not or should not change in the body of a function to which it is passed, the value should be declared const to ensure that it is not accidentally modified. There are four ways to pass a pointer to a function: - A non-constant pointer to a non-constant data: This provides the highest level of access, in which the data and the pointer can be modified. - A non-constant pointer to a constant data: This a pointer that can be modified to point any data item, but the data to which it points cannot be modified.

23 - A constant pointer to a non-constant data is a pointer that points to the same memory location. An array name is a constant pointer to the beginning of the array. All data of the array can be accessed and modified. A constant pointer to non-constant data can be used to receive an array as an argument to a function. - A constant pointer to constant data. Such a pointer always points to the same memory location, and the data at that memory location cannot be modified. If a function is only to look at an array, it should be declared as: (const datatype *var) Our earlier function is better defined as: #include <stdio.h> int minmax(const int*,int, int*); int a[] = {1,23,17,4,-5,100}; int *ptr, min, max; int main(void){ ptr= &max; min = minmax(a, 6, ptr); printf ("The max is %2d and the min is %2d", max, min);

24 return 0; } int minmax(const int *ptr_a, int n, int *max) { int mx, mn, i; mn=*ptr_a (mn=a[0]); mx= *ptr_a; for(i=0; i<n; i++) { if (mx < *ptr_a) mx= *ptr_a; if (mn > *ptr_a) mn= *ptr_a; ++ptr_a; } *max=mx; return (mn); } Here the const modifier is used to assure the user that the function will not modify the contents pointed to by the ptr_a pointer.

25 To check the effect of const within the function, we will add a statement, which attempts to change the contents of that which is pointed to by source, such as: *ptr_a = 1; which would normally change the first element of the array to the value 1 The const modifier should cause the compiler to catch this as an error. Effectively, we get an error message: Cannot modify a const object in function minmax Let s look at the bubble sort program re-written using pointers: #include <stdio.h> #define SIZE 10 void bubblesort(int *, int); int main(void) { int i, a[size] = {2, 5, 3, 8, 86, 10, 12, 43}; printf("data items in original order \n"); for(i=0; i< SIZE; i++) printf("%4d", a[i]);

26 printf("\n"); bubblesort(a, SIZE); printf("data items in ascending order \n"); for(i=0; i<size; i++) printf("%4d", a[i]); printf("\n"); return 0; } void bubblesort(int *array, int size) { int pass, j; void swap(int *a, int *b); for (pass = 1; pass <= size-1; pass++) for (j= 0; j <= size-2 ; j++) if (array[j]> array[j+1]) swap (&array[j], &array[j+1]); } void swap(int *element1ptr, int *element2ptr) { int temp; temp = *element2ptr;

27 *element2ptr= *element1ptr; *element1ptr= temp; }

28

29 Pointers and Strings: In C, strings are arrays of characters. We have seen that pointers and arrays are interchangeable and that we can use pointers to subscript an array and access its elements. We can initialize an array as: char my_string[40] = "Programming"; -When this is done, the null character ( '\0' ) is automatically appended to the end of the string.. - When this declaration is read, the compiler sets aside a contiguous block of memory 40 bytes long to hold characters and initializes it such that the first 12 characters are Programming\0. We will show a program handling characters using the pointer notation: /*Convert lowercase letters to uppercase letters*/ /*using a non-constant pointer to non-constant data*/ #include <iostream> using namespace std; void converttouppercase(char *); int main(void) { char string[]= "characters";

30 cout<<"the string before conversion is: "<< string<<endl; converttouppercase(string); cout<<"the string before conversion is: "<< string<<endl; return 0; } void converttouppercase(char *s) { while( *s!= '\0') { if (*s >= 'a' && *s <= 'z') *s -=32; ++s;}} Sample Program Output: The string before conversion is: characters The string after conversion is: CHARACTERS Let s consider the following program that defines two string copying functions: #include <iostream> using namespace std; void copy1(char *, const char *);

31 void copy2(char *, const char *); int main(void) { char string1[10], *string2= Hello ; char string3[10], string4[] = Good Bye ; copy1(string1, string2); cout<< string1= << string1<<endl; copy2(string3, string4); cout<< string3 = <<string3<<endl; return 0; } void copy1(char *s1, const char *s2) { int j; for (j=0; s1[j] = s2[j]; j++); } void copy2(char *s1, const char *s2) { for ( ; *s1 = *s2; s1++, s2++) ; }

32 Sample Program Output: string1 = Hello string3 = Good Bye Arrays of pointers: Like any other data type pointer may be arrayed. The declaration for an int pointer array of size 10 is: int *x[10]; To assign the address of an integer variable var to the third element of the array we use: x[2] =&var; To find the value of var, we use: *x[2]; A common use of arrays of pointers is to form an array of strings, or a string array. Each element of the array is a string, which in C is a pointer to its first character. The string is an array of characters. char *suit[4] ={ Hearts, Diamonds, Clubs, Spades }; *suit[4] declares an array of 4 elements, char * indicates that each element is of type pointer to char.

33 suit[0] H e a r t s \0 suit[1] D i a m o n d s \0 suit[2] C l u b s \0 suit[3] S p a d e s \0 Each pointer points to the first character of its corresponding string. Thus, even though the suit array is fixed in size, it provides access to character strings of any length. Build a table of error messages called error_table, that would include these messages: 1. Read error \n 2. Write error \n 3. Cannot open file \n 4. Cannot find file \n char *error_table[4] = { Read error \n, Write error \n, Cannot open file \n, Cannot find file \n }; Write a program that looks up a given name in an array of names. Use the function strcmp to find the name. Answer:

34 #include <iostream> #include <string.h> using namespace std; #define SIZE 50 int main(void) { char *name[7]= { "Mickey", "Minnie", "Donald", "Felix", "Stumpy", "Dilbert", "Garfield"}; char name1[size]; cout<< "Please enter the name to be searched: \n"; cin >> name1; int t; for (t=0; t<7; t++) if (strcmp(name1, name[t]) == 0) { cout<< "The name was found\n"; if (t == 7) break; }; cout<<"the name was not found\n"; return 0;}

35 Pointers to Functions: A function has a physical location in memory or an address that can be assigned to a pointer. A pointer to a function contains the address of the function in memory. A function s address is the entry point of the function. A function name is really the starting address in memory of the code that performs the function name. For example int (*functptr)(int, int); This expression declares a pointer to a function that takes two input arguments of type integer and returns an integer result. Pointers to functions can be passed to functions, returned from functions, stored in arrays. #include <iostream> #include <string.h> using namespace std; void check(char *a, char *b, int(*)(const char *, const char *)); void main(void) {

36 char s1[80], s2[80]; int (*p)(const char *, const char *); p = strcmp; cin>>s1>>s2; check(s1, s2, p); return 0;} void check(char *a, char *b, int (*cmp)(const char *, const char *)){ printf("testing for equality\n"); if (!(*cmp)(a, b)) } printf("equal"); else printf( "not equal"); Sample Program Output 1: This This testing for equality equal Sample Program Output 2:

37 This this testing for equality not equal

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

C Pointers. 7.2 Pointer Variable Definitions and Initialization

C Pointers. 7.2 Pointer Variable Definitions and Initialization 1 7 C Pointers 7.2 Pointer Variable Definitions and Initialization Pointer variables Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain

More information

C++ Programming Chapter 7 Pointers

C++ Programming Chapter 7 Pointers C++ Programming Chapter 7 Pointers Yih-Peng Chiou Room 617, BL Building (02) 3366-3603 ypchiou@cc.ee.ntu.edu.tw Photonic Modeling and Design Lab. Graduate Institute of Photonics and Optoelectronics & Department

More information

C Pointers Pearson Education, Inc. All rights reserved.

C Pointers Pearson Education, Inc. All rights reserved. 1 7 C Pointers 2 Addresses are given to us to conceal our whereabouts. Saki (H. H. Munro) By indirection find direction out. William Shakespeare Many things, having full reference To one consent, may work

More information

Chapter 6 - Pointers

Chapter 6 - Pointers Chapter 6 - Pointers Outline 1 Introduction 2 Pointer Variable Declarations and Initialization 3 Pointer Operators 4 Calling Functions by Reference 5 Using the const Qualifier with Pointers 6 Bubble Sort

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

Lecture 05 POINTERS 1

Lecture 05 POINTERS 1 Lecture 05 POINTERS 1 Pointers Powerful, but difficult to master Simulate call-by-reference Close relationship with arrays and strings Pointer Variable vs. Normal Variable Normal variables contain a specific

More information

Chapter 7. Pointers. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Chapter 7. Pointers. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 7 Pointers Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Chapter 7 - Pointers 7.1 Introduction 7.2 Pointer Variable Definitions and Initialization

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

MYcsvtu Notes LECTURE 34. POINTERS

MYcsvtu Notes LECTURE 34.  POINTERS LECTURE 34 POINTERS Pointer Variable Declarations and Initialization Pointer variables Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain

More information

C Pointers. sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then

C Pointers. sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then 1 7 C Pointers 7.7 sizeof Operator 2 sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then int myarray[ 10 ]; printf( "%d", sizeof(

More information

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Pointers and Strings Prentice Hall, Inc. All rights reserved. Pointers and Strings 1 sizeof operator Pointer Expressions and Pointer Arithmetic Relationship Between Pointers and Arrays Arrays of Pointers Case Study: Card Shuffling and Dealing Simulation sizeof operator

More information

Programming for Engineers Pointers

Programming for Engineers Pointers Programming for Engineers Pointers ICEN 200 Spring 2018 Prof. Dola Saha 1 Pointers Pointers are variables whose values are memory addresses. A variable name directly references a value, and a pointer indirectly

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

Chapter 5 - Pointers and Strings

Chapter 5 - Pointers and Strings Chapter 5 - Pointers and Strings 1 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization 5.3 Pointer Operators 5.4 Calling Functions by Reference 5.5 Using const with Pointers 5.6 Bubble

More information

Chapter 5 - Pointers and Strings

Chapter 5 - Pointers and Strings Chapter 5 - Pointers and Strings 1 5.1 Introduction 2 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization 5.3 Pointer Operators 5. Calling Functions by Reference 5.5 Using const with

More information

Pointers. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Pointers. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Pointers Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline 7.1 Introduction 7.2 Pointer Variable Definitions and Initialization 7.3 Pointer

More information

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 ARRAYS ~ VECTORS KOM3191 Object-Oriented Computer Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 What is an array? Arrays

More information

Content. In this chapter, you will learn:

Content. In this chapter, you will learn: ARRAYS & HEAP Content In this chapter, you will learn: To introduce the array data structure To understand the use of arrays To understand how to define an array, initialize an array and refer to individual

More information

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Pointers and Strings Prentice Hall, Inc. All rights reserved. Pointers and Strings 1 Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using const with Pointers Selection Sort Using Pass-by-Reference 2

More information

POINTERS. Content. Pointers. Benefits of Pointers. In this chapter, you will learn:

POINTERS. Content. Pointers. Benefits of Pointers. In this chapter, you will learn: Content POINTERS Erkut ERDEM Hacettepe University December 2010 In this chapter, you will learn: To be able to use pointers. To be able to use pointers to pass arguments to functions using call by reference.

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

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer: Chapter-11 POINTERS Introduction: Pointers are a powerful concept in C++ and have the following advantages. i. It is possible to write efficient programs. ii. Memory is utilized properly. iii. Dynamically

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

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book. Pointers and Arrays CS 201 This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book. Pointers Powerful but difficult to master Used to simulate pass-by-reference

More information

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty! Chapter 6 - Functions return type void or a valid data type ( int, double, char, etc) name parameter list void or a list of parameters separated by commas body return keyword required if function returns

More information

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c. Chapter 9: Pointers 9.1 Getting the Address of a Variable C++ Variables [ not in book ] A Variable has all of the following attributes: 1. name 2. type 3. size 4. value 5. storage class static or automatic

More information

Pointers and Strings. Adhi Harmoko S, M.Komp

Pointers and Strings. Adhi Harmoko S, M.Komp Pointers and Strings Adhi Harmoko S, M.Komp Introduction Pointers Powerful, but difficult to master Simulate call-by-reference Close relationship with arrays and strings Pointer Variable Declarations and

More information

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7 Computer Programming & Problem Solving ( CPPS ) Chapter No 7 Sir Syed University of Engineering & Technology Computer Engineering Department University Road, Karachi-75300, PAKISTAN Muzammil Ahmad Khan

More information

PROGRAMMAZIONE I A.A. 2017/2018

PROGRAMMAZIONE I A.A. 2017/2018 PROGRAMMAZIONE I A.A. 2017/2018 A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. DECLARING POINTERS POINTERS A pointer represents both

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

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

Exercise 1.1 Hello world

Exercise 1.1 Hello world Exercise 1.1 Hello world The goal of this exercise is to verify that computer and compiler setup are functioning correctly. To verify that your setup runs fine, compile and run the hello world example

More information

Chapter 7 C Pointers

Chapter 7 C Pointers Chapter 7 C Pointers Objectives of This Chapter Definition and Operations with Pointers Using Pointers to pass arguments as call by reference call. Using Pointers to deal with arrays and strings. Character

More information

Language comparison. C has pointers. Java has references. C++ has pointers and references

Language comparison. C has pointers. Java has references. C++ has pointers and references Pointers CSE 2451 Language comparison C has pointers Java has references C++ has pointers and references Pointers Values of variables are stored in memory, at a particular location A location is identified

More information

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura CS 2060 Week 6 1 Pointer Variables 2 Pass-by-reference 3 const pointers 4 Pointer arithmetic 5 sizeof 6 Arrays of pointers 7 Next Time Pointers The pointer is one of C s most powerful and important features.

More information

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary Pointers 1 2 Outline Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary 3 Computer Memory Revisited Computers store data in memory slots Each slot has an

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

Programación de Computadores. Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana

Programación de Computadores. Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana POINTERS Programación de Computadores Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana 2018-01 Pointers A pointer is a reference to

More information

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings 19/10/2017 CE221 Part 2 1 Variables and References 1 In Java a variable of primitive type is associated with a memory location

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

More Pointers Week 11

More Pointers Week 11 More Pointers Week 11 Pointer Variable Normal variables Contain a specific value (direct reference) Pointer variables Contain memory addresses as their values countptr count 7 count 7 Pointer Expressions

More information

Week 3: Pointers (Part 2)

Week 3: Pointers (Part 2) Advanced Programming (BETC 1353) Week 3: Pointers (Part 2) Dr. Abdul Kadir abdulkadir@utem.edu.my Learning Outcomes: Able to describe the concept of pointer expression and pointer arithmetic Able to explain

More information

A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables.

A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables. Lecture 9 Pointers A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables. This variable may be of type int, char,

More information

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015 CS 141 - Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015 You may take this test with you after the test, but you must turn in your answer sheet. This test has the following sections:

More information

Introduction to Scientific Computing and Problem Solving

Introduction to Scientific Computing and Problem Solving Introduction to Scientific Computing and Problem Solving Lecture #22 Pointers CS4 - Introduction to Scientific Computing and Problem Solving 2010-22.0 Announcements HW8 due tomorrow at 2:30pm What s left:

More information

... Lecture 12. Pointers

... Lecture 12. Pointers Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 12-1 Lecture 12. Pointers Variables denote locations in memory that can hold values; arrays denote contiguous locations int i = 8, sum = -456;

More information

Homework #3 CS2255 Fall 2012

Homework #3 CS2255 Fall 2012 Homework #3 CS2255 Fall 2012 MULTIPLE CHOICE 1. The, also known as the address operator, returns the memory address of a variable. a. asterisk ( * ) b. ampersand ( & ) c. percent sign (%) d. exclamation

More information

First of all, it is a variable, just like other variables you studied

First of all, it is a variable, just like other variables you studied Pointers: Basics What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the address (rather than the value)

More information

Procedural programming with C

Procedural programming with C Procedural programming with C Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 77 Functions Similarly to its mathematical

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

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

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

Functions. Introduction :

Functions. Introduction : Functions Introduction : To develop a large program effectively, it is divided into smaller pieces or modules called as functions. A function is defined by one or more statements to perform a task. In

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

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools 1 Program Components in C++ 2 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 2 Functions and Arrays Jan 13, 200 Modules: functionsand classes Programs use new and prepackaged

More information

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each): FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each): 1. If a function has default arguments, they can be located anywhere

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

Relationship between Pointers and Arrays

Relationship between Pointers and Arrays Relationship between Pointers and Arrays Arrays and pointers are intimately related in C and often may be used interchangeably. An array name can be thought of as a constant pointer. Pointers can be used

More information

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9 Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Uppercase Alphabets Lowercase Alphabets Character Set A, B, C, Y, Z a, b, c, y, z Digits

More information

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja

More information

Pointers as Arguments

Pointers as Arguments Introduction as Arguments How it Works called program on start of execution xw = &i xf = &d after excution xw = &i xf = &d caller program i? d? i 3 d.14159 x 3.14159 x 3.14159 R. K. Ghosh (IIT-Kanpur)

More information

VARIABLES & ASSIGNMENTS

VARIABLES & ASSIGNMENTS Fall 2018 CS150 - Intro to CS I 1 VARIABLES & ASSIGNMENTS Sections 2.1, 2.2, 2.3, 2.4 Fall 2018 CS150 - Intro to CS I 2 Variables Named storage location for holding data named piece of memory You need

More information

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ... Why Pointers Pointers They provide the means by which functions can modify arguments in the calling function. They support dynamic memory allocation. They provide support for dynamic data structures, such

More information

Work relative to other classes

Work relative to other classes Work relative to other classes 1 Hours/week on projects 2 C BOOTCAMP DAY 1 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh Overview C: A language

More information

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable CS 1400 Chapter 9 9.1 Getting the Address of a Variable A variable has: Name Value Location in a memory Type The location in memory is an address Use address operator & to get address of a variable: int

More information

BITG 1113: POINTER LECTURE 12

BITG 1113: POINTER LECTURE 12 BITG 1113: POINTER LECTURE 12 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the concept of pointer. 2. Write declaration and initialization of a pointer. 3. Do arithmetic

More information

CSC 211 Intermediate Programming. Arrays & Pointers

CSC 211 Intermediate Programming. Arrays & Pointers CSC 211 Intermediate Programming Arrays & Pointers 1 Definition An array a consecutive group of memory locations that all have the same name and the same type. To create an array we use a declaration statement.

More information

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

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

A First Program - Greeting.cpp

A First Program - Greeting.cpp C++ Basics A First Program - Greeting.cpp Preprocessor directives Function named main() indicates start of program // Program: Display greetings #include using namespace std; int main() { cout

More information

Pointers. Variable Declaration. Chapter 10

Pointers. Variable Declaration. Chapter 10 Pointers Chapter 10 Variable Declaration When a variable is defined, three fundamental attributes are associated with it: Name Type Address The variable definition associates the name, the type, and the

More information

OBJECT ORIENTED PROGRAMMING

OBJECT ORIENTED PROGRAMMING OBJECT ORIENTED PROGRAMMING LAB 1 REVIEW THE STRUCTURE OF A C/C++ PROGRAM. TESTING PROGRAMMING SKILLS. COMPARISON BETWEEN PROCEDURAL PROGRAMMING AND OBJECT ORIENTED PROGRAMMING Course basics The Object

More information

CS201 Some Important Definitions

CS201 Some Important Definitions CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data

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

Lecture 9 - Pointers 1

Lecture 9 - Pointers 1 Lecture 9 - Pointers 1 Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions by Reference Pointer Expressions and Pointer Arithmetic Relationship between

More information

CS2255 HOMEWORK #1 Fall 2012

CS2255 HOMEWORK #1 Fall 2012 CS55 HOMEWORK #1 Fall 01 1.What is assigned to the variable a given the statement below with the following assumptions: x = 10, y = 7, and z, a, and b are all int variables. a = x >= y; a. 10 b. 7 c. The

More information

Reference operator (&)

Reference operator (&) Pointers Each cell can be easily located in the memory because it has a unique address and all the memory cells follow a successive pattern. For example, if we are looking for cell 1776 we know that it

More information

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body Week 3 Pointers, References, Arrays & Structures Gaddis: Chapters 6, 7, 9, 11 CS 5301 Fall 2013 Jill Seaman 1 Arguments passed by value! Pass by value: when an argument is passed to a function, its value

More information

Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin 15-123 Effective Programming in C and Unix Announcements SL2 is due Thursday 1/20 midnight Complete the Academic Honesty Form in class All code downloads are from /afs/andrew/course/15/123/download All

More information

Lecture 23: Pointer Arithmetic

Lecture 23: Pointer Arithmetic Lecture 23: Pointer Arithmetic Wai L. Khoo Department of Computer Science City College of New York November 29, 2011 Wai L. Khoo (CS@CCNY) Lecture 23 November 29, 2011 1 / 14 Pointer Arithmetic Pointer

More information

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents: Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language Dr. Amal Khalifa Lecture Contents: Introduction to C++ Origins Object-Oriented Programming, Terms Libraries and Namespaces

More information

Outline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples.

Outline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples. Outline Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples. 1 Introduction A pointer is a variable that contains a memory address Pointers

More information

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes Scott Gibson Pointers & Dynamic Memory Lecture #1 Office: LAH 103 Email: sgibson@brookdalecc.edu sgib@optonline.net Web: www.brookdalecc.edu/fac/cos/sgibson Phone: (732) 224 2285 1 2 Pre & Co Requisites

More information

Objectives. In this chapter, you will:

Objectives. In this chapter, you will: Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about

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

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. CMPSC11 Final (Study Guide) Fall 11 Prof Hartman Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) This is a collection of statements that performs

More information

Pointers, Arrays and C-Strings

Pointers, Arrays and C-Strings Pointers, Arrays and C-Strings Data Processing Course, I. Hrivnacova, IPN Orsay Variable in Memory Pointers Nullptr Pointers vs References C-Arrays C-Strings Problems with C-Arrays, C-Strings 1 Variables

More information

What is an algorithm?

What is an algorithm? Announcements CS 142 C++ Pointers Reminder Program 6 due Sunday, Nov. 9 th by 11:55pm 11/3/2014 2 Pointers and the Address Operator Pointer Variables Each variable in a program is stored at a unique address

More information

DECLARAING AND INITIALIZING POINTERS

DECLARAING AND INITIALIZING POINTERS DECLARAING AND INITIALIZING POINTERS Passing arguments Call by Address Introduction to Pointers Within the computer s memory, every stored data item occupies one or more contiguous memory cells (i.e.,

More information

Variation of Pointers

Variation of Pointers Variation of Pointers A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before

More information

Pointers. Reference operator (&) ted = &andy;

Pointers. Reference operator (&)  ted = &andy; Pointers We have already seen how variables are seen as memory cells that can be accessed using their identifiers. This way we did not have to care about the physical location of our data within memory,

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

A Fast Review of C Essentials Part I

A Fast Review of C Essentials Part I A Fast Review of C Essentials Part I Structural Programming by Z. Cihan TAYSI Outline Program development C Essentials Functions Variables & constants Names Formatting Comments Preprocessor Data types

More information

Lecture 04 Introduction to pointers

Lecture 04 Introduction to pointers Lecture 04 Introduction to pointers A pointer is an address in the memory. One of the unique advantages of using C is that it provides direct access to a memory location through its address. A variable

More information

Lecture on pointers, references, and arrays and vectors

Lecture on pointers, references, and arrays and vectors Lecture on pointers, references, and arrays and vectors pointers for example, check out: http://www.programiz.com/cpp-programming/pointers [the following text is an excerpt of this website] #include

More information

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University Fundamental Data Types CSE 130: Introduction to Programming in C Stony Brook University Program Organization in C The C System C consists of several parts: The C language The preprocessor The compiler

More information

Tokens, Expressions and Control Structures

Tokens, Expressions and Control Structures 3 Tokens, Expressions and Control Structures Tokens Keywords Identifiers Data types User-defined types Derived types Symbolic constants Declaration of variables Initialization Reference variables Type

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