-
Notifications
You must be signed in to change notification settings - Fork 20
Assignment 7 #8
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?
Assignment 7 #8
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,25 @@ | ||
| Ava N. | ||
| July 7, 2015 | ||
|
|
||
| Assignment 7 | ||
|
|
||
| 1. ++*p increments value at location p point to, then evaluates (stores) to incremented value. | ||
| *p++ evaluates the value at location p points to, then advances p. (*p)++ evaluates the value at the location p points to, then increments that value. | ||
|
|
||
|
|
||
| 2. The left to right or right to left order for operator precedence is not guarenteed. | ||
|
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! 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. The advantages of using pointers are that they provide a way for functions to modify their calling arguments, pointers support dynamic memory allocation, and they increase efficiency when working with arrays. | ||
|
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! they also permit references to functions! |
||
|
|
||
|
|
||
| 4. (*int, &char, 10, 0 or 1, ) (looking at the types: *X==y answer=o or 1 or invalid) | ||
| 4.1: char * | ||
|
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! |
||
| 4.2: 1 | ||
|
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! "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: 1 | ||
|
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. perfect! |
||
| 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! remember that we asked for integer evaluations when possible. |
||
| 4.5: int * | ||
|
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! |
||
| 4.6: int | ||
|
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. (remember that we asked for integer evaluations whenever possible) |
||
| 4.7: 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.
|
||
| 4.8: char* | ||
|
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! |
||
| 4.9: invalid | ||
|
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. 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. The format of a function pointer goes like this:
|
||
| 4.10: invalid | ||
|
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. Like I said in class, this is just a definition question, so the answer is 8 because pointers always allocate 8 bytes.
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. PLEASE NOTE: Add explanations to your answers! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| //Ava N. | ||
| //In a program reverse.c, write a string reversal function using pointers that takes only a string as input, creates 2 pointers, performs a string reversal using those pointers, and prints out the reversed string. Your driver should take a string input by the user using fgets(). | ||
|
|
||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| int main(){ | ||
| char arr[50]; | ||
| char *p1; | ||
| char *p2; | ||
| char temporary; | ||
|
|
||
| printf("Input a string that is under 50 letters long. \n"); | ||
|
|
||
| fgets(arr, sizeof(arr), stdin); | ||
| printf("\n"); | ||
| p1 = &arr; /* points to first character in string */ | ||
| p2 = &arr[strlen(arr)-1]; /* points to last character in string */ | ||
|
|
||
|
|
||
| /* Diagnostic purposes ... | ||
|
|
||
| printf("\nThe following is diagnostic *****\n"); | ||
| printf("You input: %s\n",arr); | ||
| printf("Sizeof(arr) is %d\n",sizeof(arr)); | ||
| printf("String Length (including null) is: %d\n",strlen(arr)); | ||
| printf("first character is: %c\n",arr[0]); | ||
| printf("last character is: %c\n",arr[strlen(arr)-2]); | ||
| printf("P1 and P2 point to char: %c %c\n",*p1,*p2); | ||
| printf("arr[0] is: %c\n",arr[0]); | ||
| printf("p1 address is %d\n",p1); | ||
| printf("p2 address is %d\n",p2); | ||
| printf("end diagnostic!**** \n\n"); | ||
| */ | ||
| /* End diagnostic */ | ||
|
|
||
|
|
||
|
|
||
|
|
||
| while (p1 <= p2){ | ||
| temporary = *p2; | ||
| *p2 = *p1; | ||
| *p1 = temporary; | ||
| *p1++; | ||
| *p2--; | ||
| } | ||
|
|
||
| printf("Here is the reversed of what you wrote: %s\n", arr); | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| //Ava N. | ||
| //Write a program that implements 2 functions: (strcat() and) strcmp(). Use pointers and write the main function to execute both of these function on 2 strings. | ||
|
|
||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| char *characters(char* var1, char* var2) { | ||
|
|
||
| return characters; | ||
| } | ||
| //strcmp() | ||
| //strcat() | ||
|
|
||
|
|
||
| int main (){ | ||
| //int characters; | ||
| char *var1="Welcome"; | ||
| char *var2="NY!"; | ||
|
|
||
| //); | ||
| //characters = strcmp(var1, var2); | ||
|
|
||
| printf("Text from var1 + var2: |%s %s|\n", var1, var2); | ||
|
|
||
| return(0); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
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).