-
Notifications
You must be signed in to change notification settings - Fork 20
Aly Milich Assignment 7 #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| Aly Milich | ||
|
|
||
| 1. ++*p and *++p both increment the value of the array that the pointer is pointing to whereas *p++ moves the pointer over. | ||
|
|
||
| 2. Left to right. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The simple answer to this question is neither. C doesn’t always evaluate left-to-right or right-to-left. Generally, function calls are evaluated first, followed by complex expressions and then simple expressions. |
||
|
|
||
| 3. Pointers make it easier to use arrays to change the values of certain spots. Since you don't need the for loop, it decreases the algorithmic complexity of your code and takes up less space in the computer & is easier to run. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right! Pointers are also useful because they allow references to functions and support dynamic memory management! |
||
|
|
||
| 4.1 char array | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. close! We're looking for the specific data type, which is |
||
| 4.2 Invalid -- "xyz" is not in the array | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually valid! "xyz" is just an array of characters, so the "xyz"[1] is just accessing "y". Then you just subtract 'y' from it to get 0. This is because, if you can recall, a char is just a value mapped to a character! |
||
| 4.3 Invalid -- \0 will never equal 0 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is perfectly valid! '\0' is just a NULL terminator and by definition, NULL is equal to 0. So this would evaluate as true, which is 1 in C. |
||
| 4.4 Pointer | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the answer is 10 because it's just pointing to the first element of the array! |
||
| 4.5 Address is 0 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 4.6 pointer | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since a is a pointer to the first element of the array, the increment by 2 would move the pointer two spots over to 12. so 12 is the answer. |
||
| 4.7 Address is still 0 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 4.8 Pointer | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Close, but not quite! It's actually``char*. This is because the * in ++argv dereferences the char* argv, leading to char **. The incrementing actually does nothing. In the same way that int x = 1; ++x would still be an int!` |
||
| 4.9 Invalid | ||
| 4.10 5 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| char cat(char *a, char *b){ | ||
| char *point = a; | ||
|
|
||
| while(*point != '\0'){ | ||
| point ++; | ||
| } | ||
|
|
||
| while (*b!= '\0'){ | ||
| *point = *b; | ||
| point ++; | ||
| b++; | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| char comp(char *c, char *d){ | ||
|
|
||
| char string1[10]; | ||
| char string2[10]; | ||
|
|
||
| *c = string1[10]; | ||
| *d = string2[10]; | ||
|
|
||
| int i, length1, length2; | ||
|
|
||
| length1 = strlen(string1); | ||
| length2 = strlen(string2); | ||
|
|
||
|
|
||
| for(i=0; length1<length2; i++){ | ||
| if(string1[i] == string2[i]){ | ||
| printf("0"); | ||
| i++; | ||
| } | ||
| else{ | ||
| printf("1"); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| int main(){ | ||
| char a[] = "aly "; | ||
| char b[] = "milich"; | ||
|
|
||
|
|
||
| cat(a,b); | ||
| printf("%s\n", a); | ||
|
|
||
| char c[] = "atm"; | ||
| char d[] = "atm"; | ||
|
|
||
| comp(c,d); | ||
|
|
||
| return 0; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* Reverse a string | ||
| @author Aly Milich | ||
| */ | ||
|
|
||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| int reverse(char arr[]){ | ||
| char *ptr1 = arr; | ||
|
|
||
| char *ptr2 = ptr1 + strlen(arr) -1; | ||
|
|
||
| int i; | ||
|
|
||
| for(i=0; *ptr1!=*ptr2; i++){ | ||
| printf("%c", *ptr2); | ||
| --ptr2; | ||
| } | ||
| } | ||
|
|
||
| int main(){ | ||
|
|
||
| char string[] = "MILICH\0"; | ||
|
|
||
| reverse(string); | ||
|
|
||
| return 0; | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The expression ++*p has two operators of same precedence, so compiler looks for associativity. Associativity of operators is right to left. Therefore the expression is treated as ++(*p). The expression *p++ is treated as *(p++) as the precedence of postfix ++ is higher than *. The expression *++p has two operators of same precedence, so compiler looks for associativity. Associativity of operators is right to left. Therefore the expression is treated as *(++p).