-
Couldn't load subscription status.
- Fork 20
Add files via upload #9
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,25 @@ | ||
| 1. | ||
| The difference between ++*P, *p++ and *++p is that: | ||
| ++*P increases the value of *p by one | ||
| *p++ moves up the location of the pointer by one | ||
| *++p will do exactly what ++*p does | ||
|
|
||
| 2. | ||
| Yes, the order is guaranteed | ||
|
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, actually. 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. | ||
| For fuction parameters | ||
| for dynamic programming | ||
| Pointer enables you to do things that are otherwise impossible, it also speeds up the operation | ||
|
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. | ||
| 4.1 invalid because b and c are undeclared. | ||
|
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 just a string, which is a character array, so the answer is |
||
| 4.2 invalid because of undeclared variables. | ||
|
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 I am not sure what this means. | ||
|
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. '\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 10 because the pointer of a string prints the first number. | ||
|
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.5 10 because a[0] = 10. | ||
|
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! for this question, we're just looking for the data type, so it actually returns the type |
||
| 4.6 12 because a = 10 and *p = a + 2. | ||
|
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. hm, not quite! 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 invalid because p has not been declared although *p is. | ||
|
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 ello because argv *argv is moved up one location it will not print the h. | ||
|
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. not quite! It's actually |
||
| 4.9 I am not sure what this will output. | ||
|
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 10 because that is the size of the string. | ||
|
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 - Like I said in class, this is just a definition question, so the answer is 8 because pointers always allocate 8 bytes. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| void swap() { | ||
|
|
||
| char string[50]; | ||
| printf("Enter your string"); | ||
| fgets(string, 50, stdin); | ||
|
|
||
| char *stringPtr = string; | ||
| char *reverseString = &string[strlen(string)]; | ||
| } | ||
|
|
||
| int main() { | ||
| while (*stringPtr != *reverseString) { | ||
| printf("%c", *(--reverseString)); } | ||
|
|
||
| printf("\n"); | ||
| 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).