-
Couldn't load subscription status.
- Fork 20
Add files via upload #23
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,45 @@ | ||
| Andrew Zihenni | ||
| In a text file `assignment7.txt`, answer the following questions: | ||
| 1. Explain the difference between ++*p, *p++ and *++p, if there is any. | ||
| ++*p increments only the value of the pointer of p. | ||
| *p++ points to the incremented value of p. | ||
| *++p does the same thing as *p++. | ||
|
|
||
| 2. Is the left to right or right to left order guaranteed for operator precedence? | ||
| - C does not always evaluate left-to-right or right-to-left. | ||
| - Generally, function calls are evaluated first, followed by complex expressions and then simple expressions. | ||
|
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. What are the advantages of using pointers? | ||
| Pointers are important for larger data amounts. | ||
| They are necessary for dynamic memory location, many data structures, and efficient handling of these larger data amounts. | ||
| Without pointers, one would have to store all data globally, making this extremely tedious and inefficient. | ||
|
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. Consider a C program’s main() function that starts as follows: | ||
| ``` C | ||
| int main(int argc, char **argv) { | ||
| int a[10] = { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 }; | ||
| int *p = a + 2; | ||
| char *str = "hello"; | ||
| ``` | ||
| - For integer expressions (i.e., the expressions whose types are char, short, int, size_t, long, or long long--either signed or unsigned), write the actual number value. | ||
| - For non-integer expressions, write the type name, in the format that you use to declare a variable of that type. Some example type names include but are not limited to: | ||
|
|
||
| ```C | ||
| int * | ||
| double | ||
| double ** | ||
| int(*)(int) | ||
| ``` | ||
| - Write "invalid" if a given expression is not a valid C expression. | ||
| - Make sure sure to explain each of your answers. | ||
|
|
||
| 4.1 "abc" <br>: invalid | ||
|
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 char array. So the data type is |
||
| 4.2 "xyz"[1] - ’y’ <br>: not sure | ||
|
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 ’\0’ == 0 <br>: not sure | ||
|
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 *a <br>: pointer to value of a | ||
|
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 &a[0] <br>: invalid | ||
|
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 *p <br>: pointer to value of p | ||
|
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 &p <br>: This is a non-integer expression. | ||
|
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 *++argv <br>: value = 1 | ||
|
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``char*. This is because the * in ++argv dereferences the char* argv, leading to char **. The incrementing actually does nothing. In the same way that int x = 1; ++x would still be an int!` |
||
| 4.9 &main <br>: invalid | ||
|
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 sizeof(str) <br>: value = 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. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /* Did code with Justin, had some troubles with getting it to work/run */ | ||
| #Include <stdio.h> | ||
| #Include <string.h> | ||
| Void swap () { | ||
| Char string[50]; | ||
| Printf (“Enter your string”); | ||
| Fgets (string, 50, stdin(; | ||
| Char (stringPtr = string; | ||
| Char *reserveString = &string(strlen(string)[]; | ||
|
|
||
| } | ||
| int main () { | ||
| while (*stringPtr != *reverseString()) { | ||
| printf (“%c”, *(--reverseString\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.
Nice! 💯