Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions assignment7.txt
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++.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! 💯


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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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 char *

4.2 "xyz"[1] - ’y’ <br>: not sure
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually returns the type int*. Recall that a[0] would be of type int and the ampersand just returns the memory address. So &a[0] would return the pointer to the address (since a pointer is basically an address, which is why all pointers are 8 bytes no matter what they're pointing to), which would be of type int*.

4.6 *p <br>: pointer to value of p
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The answer is actually int** for the a similar reason as number 5. The address of p returns a pointer to the address, but since p is of type int*, that means it will return the memory address of where the memory address is stored - in other words, a pointer to a pointer.

4.8 *++argv <br>: value = 1
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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:

int (*POINTER_NAME)(int a, int b)

So the answer is actually int(*)(int, char**). All you needed to do was evaluate the data types of the &main. Since & returns the memory address, main's data type would change from int to int*. Since argc and argv are just the parameters, they would remain the same data types. So the final answer is int(*)(int, char**).

4.10 sizeof(str) <br>: value = 5
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

18 changes: 18 additions & 0 deletions return.c
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;
}
}