-
Couldn't load subscription status.
- Fork 20
emma Homework #5
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,22 @@ | ||
|
|
||
|
|
||
| 1. ++*p increments what the pointer points to whereas the *p++ actually increases the value of the pointer. *++p is also increases what the pointer points to. | ||
|
|
||
|
|
||
| 2. Oh the left is guaanteed because its higher in the order of operations. | ||
|
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 allow easier use with arrays. You can go through the pointer without the for loop and it allows easier access outside of the main function. | ||
|
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 more efficient and can return multiple values from a function. |
||
|
|
||
|
|
||
| 4.1 char 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. Close! As I said in class today, we're looking for the data type, which is char*. |
||
| 4.2 Invalid becaus xyz in quotes is not an arry | ||
|
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 becuse that will never be true | ||
|
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 | ||
|
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 | ||
|
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 | ||
|
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 adress is 0 becuase we haven't moved it anywhere only increased the value at a point | ||
|
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 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. Close! you just forgot to include char. |
||
| 4.9 Invalid, you can access main like that because it isn't 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. 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 5 | ||
|
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. |
||
| I don't want to edit this now that we're going over it, but I just didn't understand what the questions were asking.... | ||
|
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. Let me know if you understand now! 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. Thank you so much for the detailed explanations!! They really helped me and I know that probably took so much time. Thank you!! Emma Ladouceur
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| #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 compare(char *c, char *d){ | ||
| char string1[10]; | ||
| char string2[10]; | ||
|
|
||
| *c = string1[10]; | ||
| *d = string2[10]; | ||
|
|
||
| int i, len1, len2; | ||
|
|
||
| len1 = strlen(string1); | ||
|
|
||
| len2 = strlen(string2); | ||
|
|
||
| for(i=0; len1<=len2; i++){ | ||
|
|
||
| if(string1[i] == string2[i]){ | ||
|
|
||
| printf("0"); | ||
| i++; | ||
| } | ||
|
|
||
| else{ | ||
| printf("1"); | ||
| break; | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
| } | ||
|
|
||
|
|
||
| int main(){ | ||
| char a[] = "Emma"; | ||
| char b[] = "Ladouceur"; | ||
| cat(a, b); | ||
| printf("%s\n", a); | ||
| char c[]="Emma"; | ||
| char d[]="Emma"; | ||
|
|
||
| compare(c,d); | ||
| return 0; | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| //Emma Ladouceur | ||
|
|
||
|
|
||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
|
|
||
| int reverse(char arr[]){ | ||
|
|
||
|
|
||
| char *ptrStart = arr; | ||
|
|
||
| char *ptrEnd = ptrStart + strlen(arr)-1; | ||
|
|
||
| int i; | ||
| for(i=0; *ptrStart != *ptrEnd; i++){ | ||
| printf("%c", *ptrEnd); | ||
| --ptrEnd; | ||
|
|
||
| } | ||
|
|
||
| } | ||
| int main(){ | ||
| printf("Enter a string of no more than 100 letters\n"); | ||
| char string[100]; | ||
| fgets(string, sizeof(string), stdin); | ||
| reverse(string); | ||
|
|
||
|
|
||
|
|
||
| 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).