-
Notifications
You must be signed in to change notification settings - Fork 20
Assignment 7 #13
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 #13
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 @@ | ||
| Olivier Gabison | ||
|
|
||
| 1. ++*p dereferences the value that the p variable is pointing to and then increments it. While *p++ increments the pointer, | ||
| and then dereferences it. Finally, *++p first increments the pointer and then dereferences it. | ||
|
|
||
| 2. No, order of precedence is not done through left to right order. | ||
|
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! 💯 |
||
|
|
||
| 3. I think the best uses of pointers is passing by reference and dynamic starting sizes on 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 allow references to functions and reduce complexity of programs! |
||
|
|
||
| 4.1 char *, its a string, and char[] has '\0' at the end of it. | ||
|
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.3 1 - the ASCII value of '\0' is 0. Therefore, 0 = 0, which returns 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. 💯 |
||
| 4.4 int, dereference of pointer a | ||
|
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! for integer values, you should have evaluated it (so the answer is 10) |
||
| 4.5 int *, it creates a pointer that is used upon a[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. right! |
||
| 4.6 int, dereference of pointer p | ||
|
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! again, you should have evaluated it (so the answer is 12) |
||
| 4.7 int **, creates a pointer for pointer p | ||
|
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 *, derefernce of the double pointer 'argv' | ||
|
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.9 This does not work as you cannot cast a pointer to a function | ||
|
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:
So the answer is actually |
||
| 4.10 5 (only when string.h is included) | ||
|
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. It's actually 8 because pointers are allocated 8 bytes regardless of the size! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #include <stdio.h> | ||
|
|
||
| int main(){ | ||
|
|
||
| char word[100] = "HELLOHH"; //THIS IS THE WORD THAT WILL BE REVERSED | ||
| char *Start = word - 1; | ||
| char *End = Start + strlen(word); | ||
| int i; | ||
| for (i=0; *Start!=*End; i++) { | ||
| printf("%c",*End); | ||
| End--; | ||
| } | ||
| printf("\n"); | ||
|
|
||
|
|
||
|
|
||
| return 0; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| #include <stdio.h> | ||
|
|
||
| int strcmp(char *a, char *b){ | ||
| if(*a > *b){ | ||
| return 1; | ||
| } else { | ||
| if (*a < *b){ | ||
| return -1; | ||
| } else { | ||
| a++; | ||
| b++; | ||
| return strcmp(a, b); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| char *strcat(char *a, char *b){ | ||
| char *answer[100]; | ||
| int i; | ||
|
|
||
| for(i = 0; i < 100; i++){ | ||
| if(*a == '\0'){ | ||
| break; | ||
| } | ||
| answer[i] = *a; | ||
| a++; | ||
| } | ||
|
|
||
| for (int j = i; i < 100; j++){ | ||
| if(*b == '\0'){ | ||
| break; | ||
| } | ||
| answer[j] = *b; | ||
| b++; | ||
| } | ||
|
|
||
| return answer; | ||
| } | ||
|
|
||
| int main(){ | ||
|
|
||
| char s1[] = "Hello"; | ||
| char s2[] = "Hallo"; | ||
|
|
||
| printf("%s", strcat(s1,s2)); | ||
|
|
||
| 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.
Perfect answer! 💯