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
21 changes: 21 additions & 0 deletions assignment7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
1. Explain the difference between ++*p, *p++ and *++p, if there is any.
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 increments the value that p is pointing to and stores that new value at p. *p++
Stores the value of p, then moves the pointer. I don't know what the last one does.
2. Is the left to right or right to left order guaranteed for operator precedence?
Not neccesarily, it depends what operators are being used. If a postfix is being used
Copy link
Contributor

Choose a reason for hiding this comment

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

Right! 💯

then i's right to left, but otherwise it is right to left.
3. What are the advantages of using pointers?
Although shockingly, mind-bogglingly annoying, pointers can be helpful and more efficient
Copy link
Contributor

Choose a reason for hiding this comment

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

lawl

when used with arrays for the purposes of returning arrays from functions or just
Copy link
Contributor

Choose a reason for hiding this comment

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

but right! they also allow pointers to functions!

managing arrays in general.
4.
4.1 "abc" char name[]
Copy link
Contributor

Choose a reason for hiding this comment

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

close! we're looking for the specific data type, so it's char*

4.2 "xyz"[1] - ’y’ ?
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 0
Copy link
Contributor

Choose a reason for hiding this comment

The 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 *a 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 &a[0] int
Copy link
Contributor

Choose a reason for hiding this comment

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

Close! 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 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 &p int
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 ?
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 ?
4.10 sizeof(str) int
Copy link
Contributor

Choose a reason for hiding this comment

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

right! but like I said in class, this is just a definition question, so the answer is 8 because pointers always allocate 8 bytes.

25 changes: 25 additions & 0 deletions reverse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdio.h>
#include <string.h>

void sReverse(char a[])
{
char *ptrStart = a;
char *ptrEnd = ptrStart + strlen(a) - 1;
int i;
for (i = 0; *ptrStart != *ptrEnd; i++)
{
printf("%c", *ptrEnd);
ptrEnd--;
}
printf("%c", a[0]);
}

int main()
{
printf("Enter a string. Through some shenanigans I don't understand, it should be reversed.\n");
char word[100];
fgets(word, sizeof(word), stdin);
sReverse(word);
return 0;
}