[Home]

# CS 223 - Spring 2017. 1/30

## Welcome to CS 223!

### Logical problem of the day

lp0125.c revisited:
```int main(){
int a[10] = {0};
for (int i = 0; i < 10; i++){
a[i] = i++;
printf("a[%d]: %d\n", i, a[i]);
}
puts("----");
for (int i = 0; i < 10; i++){
printf("a[%d]: %d\n", i, a[i]);
}
}
```
What is interesting (or disturbing) about the above code?

## Lecture 5: Introduction to C: more strings and pointers

• Office hours:
We have added the public tests for assignment 2. The office hours are Sun/Tue/Thu 8-11 PM at Hillhouse 17 Rm 111.

• Notetaker needed for this course immediately: \$12.75 / hour. If you are interested, please email Carolyn Barrett (carolyn.barrett@yale.edu) at the Resource Office on Disabilities specifying the name of this course (CSPC 223) and the professor (Stephen Slade).

• On Wednesday, we have a guest speaker: Michael Ljung, Managing Director, Accenture, Yale '92.
Lessons learned in program management, technology, and innovation at Healthcare.gov
• Professor Angluin found a wireless mouse device in DL 220. Contact her directly if it is yours.

### Assignment 2

Problem set 2

### Lecture Starts Here

#### Strings

Aspnes: Strings

• Strings are a sequence or array of characters. Strings may be delimited (using a specific termination character) or counted (specifying a numeric length). C strings are delimited by the null character, '\0'. This allows simple pointer manipulation of strings.
• This convention is not without problems: The Most Expensive One-byte Mistake
• String constants: \n, \r, \a. Two string constants separated by whitespace are combined: "foo" "bar" == "foobar"
• String buffers: hi.c
• String buffers and the perils of gets: getLine.c
• Operations on strings.
```void
strcpy2(char *dest, const char *src)
{
/* This line copies characters one at a time from *src to *dest. */
/* The postincrements increment the pointers (++ binds tighter than *) */
/*  to get to the next locations on the next iteration through the loop. */
/* The loop terminates when *src == '\0' == 0. */
/* There is no loop body because there is nothing to do there. */
while(*dest++ = *src++);
}
```
Because C pointers act exactly like array names, you can also write strcpy2 using explicit array indices. The result is longer but may be more readable if you aren't a C fanatic.
```char *
strcpy2a(char *dest, const char *src)
{
int i;

for(i = 0; src[i] != '\0'; i++) {
dest[i] = src[i];
}

/* note that the final null in src is not copied by the loop */
dest[i] = '\0';

return dest;
}
```
A similar operation to strcpy is strcat. The difference is that strcat concatenates src on to the end of dest; so that if dest previously pointed to "abc" and src to "def", dest will now point to "abcdef". Like strcpy, strcat returns its first argument. A no-return-value version of strcat is given below.
```void
strcat2(char *dest, const char *src)
{
while(*dest) dest++;
while(*dest++ = *src++);
}
```
• Length of a string:
```int
strlen(const char *s)
{
int i;

for(i = 0; *s; i++, s++);

return i;
}
```
Note issue of strlen tarpit.
• Comparing strings:
```int
strcmp(const char *s1, const char *s2)
{
while(*s1 && *s2 && *s1 == *s2) {
s1++;
s2++;
}

return *s1 - *s2;
```
Negative if s1 < s2, positive if s1 > s2, 0 if s1 == s2.

if(strcmp(s1, s2) == 0) or if(!strcmp(s1, s2))

• Dynamic string allocation:
```char *
strdup(const char *s)
{
char *s2;

s2 = malloc(strlen(s)+1);

if(s2 != 0) {
strcpy(s2, s);
}

return s2;
}
```
• Command line arguments: printArgs.c

#### Pointers

Aspnes: Pointers

• Computer memory addresses are positive integers. Actually, 0 is also a memory address, but generally something went wrong if you are trying to access memory location 0. Negative addresses are also trouble.
• Use a * to declare a variable a pointer.
```int *pointerToInt;
double *pointerToDouble;
char *pointerToChar;
char **pointerToPointerToChar;
```
• Use a & to access the address of a variable.
```int n;              /* an int variable */
int *p;             /* a pointer to an int */
p = &n;             /* p now points to n */
```
• Let's revisit the getLine program from above. mygetLine.c We have added wrappers for malloc, realloc, and free. The new functions, mymalloc, myrealloc, and myfree, report the addresses that they hand out or recycle in memory, as long as the debugflag is set.
• We can also use valgrind to detect memory leaks.
• There are different sections of memory. The Basic Service Set (BSS) hold statically allocated variables and code. The heap is used for dynamically allocated variables, e.g., from malloc. The stack is used for local variables and function calls. See lookingAtPointers.c
• Pointers allow you to get around the C convention of copying function arguments. See badDoubler.c and goodDoubler.c

[Home]