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
16 changes: 16 additions & 0 deletions assignment7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
1)
++*p increments the value that p is pointing to
Copy link
Contributor

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

*p++ gets the value that p is pointing to, then increments p
*++p increments p, then returns the new value that p is pointing at
2) yes?
Copy link
Contributor

Choose a reason for hiding this comment

The 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) You can pass by reference
Copy link
Contributor

Choose a reason for hiding this comment

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

Pointers are more efficient in handling arrays and data tables
They can be used to return multiple values from a function via function arguments
Pointers permit references to functions
Pointers allow C to support dynamic memory management
Pointers reduce length and complexity of programs

4.1) char *
Copy link
Contributor

Choose a reason for hiding this comment

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

💯

4.2) 121
Copy link
Contributor

Choose a reason for hiding this comment

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

"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) 1
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

4.4) 10
Copy link
Contributor

Choose a reason for hiding this comment

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

💯

4.5) int *
Copy link
Contributor

Choose a reason for hiding this comment

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

💯

4.6) 12
Copy link
Contributor

Choose a reason for hiding this comment

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

💯

4.7) int **
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

4.8) char *
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

4.9) invalid: we never learned about pointers to functions
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 true, but it's still valid. int(*)(int, char**).

4.10) 6
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.

31 changes: 31 additions & 0 deletions reverse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* Harry Brickner
* Given a string, will print out the reversed string (minus the newline character at the end, if there is one.) */
#include <stdio.h>
#include <string.h>

void swap (char* a, char* b){
char temp = *a;
*a = *b;
*b = temp;
}

void reverse(char str[], int length){
if(str[length - 1] == '\n') str[--length] = '\0';
for(int i = 0; i < length / 2; i++){
swap(&str[i], &str[length - i - 1]);
}
}

void driver(){
int length = 50;
char input[50];
printf("Please input a string\n");
fgets(input, length, stdin);
reverse(input, strlen(input));
printf("Reversed string is:\"%s\"\n", input);
}

int main(){
driver();
return 0;
}