22
Pointers for arrays and strings 1/24/20

Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

  • Upload
    others

  • View
    16

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Pointers for arrays and strings

1/24/20

Page 2: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Mystery functions!

Page 3: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Pointers as arrays

• Just like with new in Java, malloc can allocate space for more than one data item:

int* a = (int*) malloc(5*sizeof(int));

• Then can access the items with array syntax:a[2] = a[1];

Page 4: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Strings in C

• Just an array of chars with a 0 at the end (not ‘0’)– Type is a char*

• For “methods”, use functions from string.h

• Things to remember:– Allocate room for the \0– Scanf (with %s) takes a char*, not a char**

Page 5: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Which of the following lines does not do as its comment says?

#include <stdio.h> //for malloc (line 1)...int* p = (int*) malloc(10); //allocate 10 ints (line 2)char* s = (char*) malloc(6); //allocate room for “David” (line 3)

A. Line 1 does not match its commentB. Line 2 does not match its commentC. Line 3 does not match its commentD. More than 1 line does not match its commentE. All lines match their comments

Page 6: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Which of the following lines does not

do as its comment says?

#include <stdio.h> //for malloc (line 1)

...

int* p = (int*) malloc(10); //allocate 10 ints (line 2)

char* s = (char*) malloc(6); //allocate room for “David” (line 3)

A. Line 1 does not match its comment

B. Line 2 does not match its comment

C. Line 3 does not match its comment

D. More than 1 line does not match its comment (1, 2, and

E. All lines match their comments arguably 3)

Page 7: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Which of the following is true for the code below?

int nums[10];char* s = (char*) malloc(100 * sizeof(char));char* t = s;A. free should not be called on numsB. free should not be called on sC. free should not be called on tD. More than one statement above is trueE. None of the statements above are truefree can be called on either s or t, but not both

Page 8: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Which of the following is true for the code below?

int nums[10];char* s = (char*) malloc(100 * sizeof(char));char* t = s;A. free should not be called on numsB. free should not be called on sC. free should not be called on tD. More than one statement above is trueE. None of the statements above are true(free can be called on either s or t, but not both)

Page 9: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Which of the following is a problem

with the code below?

int* f(int x, double* y) {

int z = 23;

if(x < *y)

z = (int) (z - *y);

return &z;

}

A. Type error involving argument y

B. Type mismatch between signature and return value

C. Shouldn’t return a pointer to a local variable

D. Other syntax error

E. The code is fine (albeit not useful)

Page 10: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Which of the following is a problem

with the code below?

int* f(int x, double* y) {

int z = 23;

if(x < *y)

z = (int) (z - *y);

return &z;

}

A. Type error involving argument y

B. Type mismatch between signature and return value

C. Shouldn’t return a pointer to a local variable

D. Other syntax error

E. The code is fine (albeit not useful)

Page 11: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Which of the following is an advantage of C’s lack of bounds checking on

arrays (relative to Java)?

A. Arrays in C support insertion (creating cells)B. Arrays in C use less memoryC. Arrays in C can be accessed fasterD. More than one of the aboveE. None of the above

Page 12: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Which of the following is an advantage of C’s lack of bounds checking on

arrays (relative to Java)?

A. Arrays in C support insertion (creating cells)B. Arrays in C use less memoryC. Arrays in C can be accessed fasterD. More than one of the above (C and marginally B)

E. None of the above

Page 13: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Which of the following lines has an error?

int x = 3;char[10] str; //Achar* p = str; //B

scanf(“%d”, &x); //Cscanf(“%s”, &str); //D

//E: Not exactly one// of the above

Page 14: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Which of the following lines has an error?

int x = 3;char[10] str; //Achar* p = str; //B

scanf(“%d”, &x); //Cscanf(“%s”, &str); //D

//E: Not exactly one// of the above (A&D)

Page 15: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Which of the following is NOT true of strings in C?

A. They all have a special character at the endB. It is possible to change characters in the

middle of themC. You cannot use + to concatenate themD. Many library functions for them require

including string.hE. You use the function sizeof instead of a

length method to determine their length

Page 16: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Which of the following is NOT true of strings in C?

A. They all have a special character at the endB. It is possible to change characters in the

middle of themC. You cannot use + to concatenate themD. Many library functions for them require

including string.hE. You use the function sizeof instead of a

length method to determine their length

Page 17: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Find first non-space

Suppose you have a string called lineFind its first non-space char (set i to its index)

int i = 0; //index into linewhile(line[i] == ‘ ‘) //advance i to 1st non-space

i++;

Page 18: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Find first non-space

Suppose you have a string called lineFind its first non-space char (set i to its index)

int i = 0; //index into linewhile(line[i] == ‘ ‘) //advance i to 1st non-space

i++;

Page 19: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Copying a substringSuppose line stores a string and word is a char*Copy the first word (up to space) starting at line[i] to word

int j = 0; //index in wordwhile((line[i] != ‘ ‘) && (line[i] != 0)) { //while in word

word[j] = line[i]; //copy next chari++; //advance indicesj++;

}word[j] = 0; //add terminating 0

Page 20: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Copying a substringSuppose line stores a string and word is a char*Copy the first word (up to space) starting at line[i] to word

int j = 0; //index in wordwhile((line[i] != ‘ ‘) && (line[i] != 0)) { //while in word

word[j] = line[i]; //copy next chari++; //advance indicesj++;

}word[j] = 0; //add terminating 0

Page 21: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Yuck!

• Both use string[index] construct– potentially lots of indices

– hard to print part of a word (requires a copy)

– arguably, lots of typing

Page 22: Pointers for arrays and strings - courses.knox.educourses.knox.edu/cs214/notes/Pointers3.pdf · Strings in C •Just an array of chars with a 0 at the end (not ‘0’) –Type is

Alternative: Pointer arithmetic

• Use a pointer into the array

• Pointer itself moves: ptr++ advances it– Can also use other arithmetic

– adding “1” moves address by 1 cell (not 1 byte)

• Access value at pointer’s location with *ptr