CS代考 CS2310 Computer Programming

Computer Science, City University of
Semester B 2021-22
CS2310 Computer Programming

Copyright By PowCoder代写 加微信 powcoder

LT11: Pointer II

Access array elements via pointers
Manage strings via pointers
Dynamic memory allocation

The NULL pointer
A special value that can be assigned to any type of pointer variable (e.g., int *a=NULL; double*b=NULL;)
A symbolic constant defined in several standard library headers, e.g.
When assigned to a pointer variable, that variable points to nothing
int *ptr1 = NULL;
int *ptr2 = 0;

Operations on pointers
Copying the address
p and q points to the same variable

Copy the address
Assignment: p = q;

We copy the content (which is an address) of q to p.

After the assignment, p and q point to the same location in memory.

Therefore, if we change *p, *q will also be changed

Operations on pointers
Copying the address
p and q points to the same variable
Copying the content
Copy the value of the variable which is pointed by the p to the variable which is pointed by q
p and q may point to different variables

Copy the content

We copy the value of the variable pointed by q to the variable pointed by p.

After the assignment, p and q still point to different locations in memory.

if we change *p, *q will not be changed as p and q points to different location in memory.

Relationship between arrays and pointers

int num[2]={40,50};
num[0]=400;
num[1]=500;
int num[2]={40,50};
int *p=num;
Equivalent to
We can use array-like notation in pointers
num is a constant pointer to the first byte of the array;
p is a pointer variable; and thus the value of p can be changed.
However, the value of num cannot be changed.
num=p; /*illegal*/

Relationship between arrays and pointers

int num[2]={40,50};
int num[2]={40,50};
p=num; /* p points to 90
++p; /*p points to 94 */
Equivalent to
++p increments the content of p (an address) by sizeof(int) bytes

Arrays and pointers
Equivalent representation Remark
num &num[0] num is the address of the 0th element of the array
num+i &(num[i]) Address of the ith element of the array
*num num[0] The value of the 0th element of the array
*(num+i) num[i] The value of the ith element of the array
(*num)+i num[0]+i The value of the 0th element of the array plus i

Example 2: summing an array
#define N 10
void main()

a+0 is the address of a[0]
a+1 is the address of a[1]
a+i is the address of a[i]

So, *(a+i) means a[i]
int a[N]={1,2,3,4,5,6,7,8,9,10};
int sum = 0;
for(int i = 0; i < N; ++i) sum+=*(a+i);//sum+=a[i]; cout << sum;/*55 is printed*/ Passing arrays to functions When an array is being passed, its base address is passed; the array elements themselves are not copied  This is call-by-pointer As a notational convenience, the compiler allows array bracket notation to be used in declaring pointers as parameters, e.g. double sum(int *a); is the same as double sum(int b[]); double sum(int *b) double total = 0.0; for (i=0; i> a string (I)
char s[]=”abc”;

input: “Hello World”

cin >> a string (I)

Size of s is 4. Array out-of-bound! cin >> does not perform bound-checking
Better to use:
cin.getline(s,4); /*read at most 3 characters*/
Leaving space for the final ‘\0’ character

cin >> a string (II)
#include
using namespace std;
void main () {
cin >> s1;
cout << s1<
void main () {
char *s1=NULL;
s1=new char[4];
cin >> s1; /*input “abc”*/
cout << s1; delete [] s1; s1=new char[6]; cin >> s1;
cout << s1; delete [] s1; Dynamic memory allocation #include
void main () {
char *s1=NULL;
s1=new char[4];
cin >> s1; /*input “abc”*/
cout << s1; delete [] s1; s1=new char[6]; cin >> s1;
cout << s1; delete [] s1; new dynamically allocates 4 bytes of memory. new returns a pointer to the 1st byte of the chunk of memory, which is assigned to s1 Example 4: Dynamic memory allocation #include
void main () {
char *s1=NULL;
s1=new char[4];
cin >> s1; /*input “abc”*/
cout << s1; delete [] s1; s1=new char[6]; cin >> s1;
cout << s1; delete [] s1; Example 4: Dynamic memory allocation #include
void main () {
char *s1=NULL;
s1=new char[4];
cin >> s1; /*input “abc”*/
cout << s1; delete [] s1; s1=new char[6]; cin >> s1;
cout << s1; delete [] s1; Memory is free and can be used to store other data #include
void main () {
char *s1=NULL;
s1=new char[4];
cin >> s1; /*input “abc”*/
cout << s1; delete [] s1; s1=new char[6]; cin >> s1;
cout << s1; delete [] s1; new dynamically allocates 6 bytes of memory. new returns a pointer to the 1st byte of the chunk of memory, which is assigned to s1 #include
void main () {
char *s1=NULL;
s1=new char[4];
cin >> s1; /*input “abc”*/
cout << s1; delete [] s1; s1=new char[6]; cin >> s1;
cout << s1; delete [] s1; #include
void main () {
char *s1=NULL;
s1=new char[4];
cin >> s1; /*input “abc”*/
cout << s1; delete [] s1; s1=new char[6]; cin >> s1;
cout << s1; delete [] s1; #include
void main () {
char *s1=NULL;
s1=new char[4];
cin >> s1; /*input “abc”*/
cout << s1; delete [] s1; s1=new char[6]; cin >> s1;
cout << s1; delete [] s1; The input file score.txt contains the scores of 3 different courses for n students. The first line of score.txt gives the value of n Reads all the scores, find all the students who have a failed score and output their scores for every course As the number of the students is read from the input, we cannot define a normal 2D array (array size is not a constant). Hence, we can use dynamic memory allocation to solve the problem ifstream in("score.txt"); if (in.fail()) {exit(1);} int **p = new int*[n]; for (int i = 0; i < n; i++) { p[i] = new int[3]; for (int j = 0; j < 3; j++) in >> p[i][j];
in.close();
for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { if (p[i][j] < 60) { for (int k = 0; k < 3; k++) cout << p[i][k] << ' ‘; cout << endl; for (int i=0; i> start fill data from here

cin >> start fill data from here

/docProps/thumbnail.jpeg

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com