Upload
others
View
16
Download
0
Embed Size (px)
Citation preview
Pointers for arrays and strings
1/24/20
Mystery functions!
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];
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**
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
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)
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
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)
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)
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)
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
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
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
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)
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
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
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++;
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++;
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
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
Yuck!
• Both use string[index] construct– potentially lots of indices
– hard to print part of a word (requires a copy)
– arguably, lots of typing
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