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
15 changes: 15 additions & 0 deletions assignment7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Rumeet Goradia
1. ++*p increments the value that is stored in the memory location to which p points and stores that value. *p++ stores the value at the location to which p points and then advances p. (*p)++ stores the value at the location to which p points and then increments that value.
Copy link
Contributor

Choose a reason for hiding this comment

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


2. The left to right or right to left order for operator precedence is partially guaranteed for operator precedence. That is, many operators will be evaluated from left to right, but some operators operate from right to left. Therefore, in a line of code which contains operators from both of these groups, the expression will be evaulated in both directions.
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. Pointers allow for dynamic memory location, meaning that if we use them as arrays, the sizes of these arrays are not static. Pointers are also useful as parameters for functions. They can be used in functions that need to change their actual parameters; in other words, they can essentially be used to pass certain variables by reference instead of by value. Furthermore, without pointers, it is impossible to return an array of values.
Copy link
Contributor

Choose a reason for hiding this comment

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

right! some more important things to note:
-Pointers permit references to functions
-They can be used to return multiple values from a function via function arguments

4. 1. char * --> "abc" is a string, which is basically a char pointer
Copy link
Contributor

Choose a reason for hiding this comment

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

perfect! 💯

2. invalid --> substracting a char from a string is not allowed in C
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!

3. invalid --> cannot compare char to int
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. 10 --> points to the first 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.

perfect!

5. p-2 --> gives address number of a[0]
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 we're looking for the data type, which is int*

6. 12 --> gives value of a[2]
Copy link
Contributor

Choose a reason for hiding this comment

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

Perfect!

7. int** --> stores memory address of a memory address of an int
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice job!

8. char* --> advances memory location, then gives value of pointer of pointer, which is pointer
Copy link
Contributor

Choose a reason for hiding this comment

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

yes!

9. int * --> gives memory location of int
Copy link
Contributor

Choose a reason for hiding this comment

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

pretty much right! You should have also given the parameter types (which would remain unchanged), but you got the gist of it correctly!

10. int --> sizeof gives integer value
Copy link
Contributor

Choose a reason for hiding this comment

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

you're right about the type, but you should have given us the standard size which is 8!


Copy link
Contributor

Choose a reason for hiding this comment

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

Great job overall!

23 changes: 23 additions & 0 deletions reverse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*Rumeet Goradia - String Reversal*/
#include<stdio.h>
#include<string.h>

void reversal (char *word)
{
char *wordStart = word;
char *wordEnd = wordStart + strlen(word) - 2;
for(;wordEnd>=wordStart;wordEnd--)
{
printf("%c", *wordEnd);
}
printf("\n");
return;
}
int main()
{
char word[100];
printf("Please input a string.\n");
fgets(word, sizeof(word), stdin);
reversal(word);
return 0;
}