-
Couldn't load subscription status.
- Fork 20
Matthew Danielson - Assignment 7 #4
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,34 @@ | ||
| Matthew Danielson | ||
| 7/7/16 | ||
| Part 2 of Assignment7 | ||
|
|
||
| 1) ++*p increments the value of a pointer, and *p++ increments the address of a pointer. *++p is not valid, as it is incrementing invalidly. | ||
|
|
||
| 2) Left to right order is guaranteed for operator precedence; without this, many operators become essentially useless, because the logic of the code would not be reliable. | ||
|
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 are passed by reference allowing for multiple return types. Additionally, constant pointers can be made; additionally, arrays of pointers allow for non-standard char arrays that are of varying size. They can also be used to return an array of values. | ||
|
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. Nice! 👍 |
||
|
|
||
| 4) a[0] = 11 | ||
| a[1] = 11 | ||
| a[2] = 12 | ||
| a[3] = 13 | ||
| a[4] = 14 | ||
| a[5] = 15 | ||
| a[6] = 16 | ||
| a[7] = 17 | ||
| a[8] = 18 | ||
| a[9] = 19 | ||
| p = 12 | ||
| char *varname[] | ||
| 4.1 char[] | ||
|
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. you're right that it's a char array, but you were supposed to write the data type, which would be char *! |
||
| 4.2 would subtract ascii values, resulting in value 1, or SOH | ||
|
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! "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 Valid, changes a char | ||
|
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. You're right that this is valid. The answer is 1. '\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 | ||
|
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. For this question, you should have evaluated it. *a returns the first element of the array, which is 10. |
||
| 4.5 Returns the address of the beginning of the array | ||
|
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 Returns the value that said pointer points to | ||
|
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 Returns the address of a pointer | ||
|
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 actually int** for the a similar reason as number 5. The address of p returns a pointer to the address, but since p is of type int*, that means it will return the memory address of where the memory address is stored - in other words, a pointer to a pointer.` |
||
| 4.8 Increments the address of the pointer | ||
|
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.9 Gets the address of the variable | ||
|
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. Functions in C are actually just pointers to a spot in the program where some code exists. Just like you've been creating pointers to structs, strings, and arrays, you can point a pointer at a function too. The main use for this is to pass "callbacks" to other functions, or to simulate classes and objects. In this exercise we'll do some callbacks, and in the next one we'll make a simple object system. |
||
| 4.10 Returns an integer length of the space in memory | ||
|
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.
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* Matthew Danielson | ||
| * 7/8/16 | ||
| * Pointers Practice | ||
| * name: exercise.c | ||
| */ | ||
| #include <stdio.h> | ||
|
|
||
| int strcmp(char[], char[]); | ||
| void strct(char[], char[]); | ||
|
|
||
| int main(){ | ||
| char *s1 = {"Hey"}; | ||
| char *s2 = {"all"}; | ||
| strct(s1,s2); | ||
| return 0; | ||
| } | ||
|
|
||
| int strcmp(char array1[4], char array2[4]){ | ||
| int toreturn = 0; | ||
| for(int x = 0; x< 20; x++){ | ||
| if(array1[x] != array2[x]){ | ||
| if(array1[x] > array2[x]) | ||
| toreturn = -1; | ||
| if(array1[x] < array2[x]) | ||
| toreturn = 1; | ||
| break; | ||
| } | ||
| return toreturn; | ||
| } | ||
| } | ||
|
|
||
| void strct(char array1[4], char array2[4]){ | ||
| char array3[sizeof(array1) + sizeof(array2)-2]; | ||
| int x = 0; | ||
| printf("%d\n", (int)sizeof(array1)); | ||
| for(; array1[x] != '\0';x++){ | ||
| array3[x] = array1[x]; | ||
| } | ||
| for(int y = 0;y<sizeof(array2); x++){ | ||
| array3[x] = array2[y]; | ||
| y++; | ||
| } | ||
| printf("%s\n", array3); | ||
|
|
||
|
|
||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* Matthew Danielson | ||
| * 7/7/16 | ||
| * reverse.c | ||
| * Takes a string and prints it in reverse | ||
| */ | ||
|
|
||
| #include <stdio.h> | ||
|
|
||
| int main(){ | ||
| int length; | ||
| char throwaway; | ||
| printf("How long will your string be?"); | ||
| scanf("%d", &length); | ||
| length ++; | ||
| scanf("%c", &throwaway); | ||
| char array[length]; | ||
| printf("\nPlease enter a string:"); | ||
| fgets(array, sizeof(array), stdin); | ||
| char *start = array; | ||
| char *end = array + sizeof(array) + 1; | ||
| while(*start != *end){ | ||
| printf("%c", *end); | ||
| *end--; | ||
| } | ||
| printf("\n"); | ||
|
|
||
| } |
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.
`Hmm, not quite: 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).``