int main(){ int a[10] = {0}; for (int i = 0; i < 10; i++){ a[i] = i++; printf("a[%d]: %d\n", i, a[i]); } }What is interesting (or disturbing) about the above code?
We have added the public tests for assignment 1. In addition, we have met ULAs and scheduled office hours. The office hours are Sun/Tue/Thu 8-11 PM at Hillhouse 17 Rm 111.
See /c/cs223/hw1. Specifically, check out the sample file /c/cs223/hw1/Total.c
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 = 0; 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 previous 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++); }
int strlen(const char *s) { int i; for(i = 0; *s; i++, s++); return i; }Note issue of strlen tarpit.
int strcmp(const char *s1, const char *s2) { while(*s1 && *s2 && *s1 == *s2) { s1++; s2++; } return *s1 - *s2;if(strcmp(s1, s2) == 0) or if(!strcmp(s1, s2))
char * strdup(const char *s) { char *s2; s2 = malloc(strlen(s)+1); if(s2 != 0) { strcpy(s2, s); } return s2; }