diff --git a/assignment7.txt b/assignment7.txt new file mode 100644 index 0000000..6dc78c5 --- /dev/null +++ b/assignment7.txt @@ -0,0 +1,23 @@ +Julia Tan + +1. The order of operations of prefix ++ and * is the same. It is read from right to left. The order of operations of postfix ++ is higher than both * and prefix ++. It is read from left to right. + +++*p would be read as ++(*p). (dereferences p then increments it) +*p++ would be read as *(p++). (increments p then dereferences it) +*++p would be read as *(p++) as well. (incremeents p then dereferences it) + +2. No it is not guaranteed, as sometimes expressions are read left to right, and other times from right to left, as seen on the example above. + +3. Pointers are useful for handling large amounts of data efficiently. They are also used to access variables in other functions without using global variables, as global variables can be confusing and difficult to debug. Other times, pointers are essential, such as in complex data structures. + +4. +4.1 char * (char * are strings) +4.2 invalid (if "xyz" is an array, you cannot subtract letters in each element) +4.3 1 ('/0' is equal to 0. when tested if its equal to 0, it is true and therefore returns 1) +4.4 10 (first value in array) +4.5 10 (first value in array) +4.6 int * (*p is an int pointer) +4.7 int ** (this makes a pointer for pointer p) +4.8 char * (dereference of the double pointer 'argv') +4.9 int * (makes a pointer for the main function) +4.10 5 (0 to 5, as \0 is also an element in strings) diff --git a/pointersStr.c b/pointersStr.c new file mode 100644 index 0000000..338f0e7 --- /dev/null +++ b/pointersStr.c @@ -0,0 +1,36 @@ +#include +#include + +int main() { + printf("Enter 2 names to sort:\n"); + char fn[2][100]; + char ln[2][50]; + char full[2][100]; + char temp[50]; + int b = 0; + char *ptr = fn[b]; + for (int a = 0; a < 2; a ++){ + ptr = fn[b]; + printf("Person %d:\n", (a+1)); + printf("First name: "); + scanf("%s", fn[a]); + printf("Last name: "); + scanf("%s", ln[a]); + while (*ptr != '\0'){ + ptr++; + } + *ptr = ' '; + *ptr ++; + *ptr = '\0'; + strcat(fn[a], ln[a]); + printf("Full name: %s\n\n", fn[a]); + b ++; + } + printf("Here are the names in alphabetical order:\n"); + int compare = strcmp(fn[0], fn[1]); + if (compare > 0) + printf("%s\n%s\n",fn[1], fn[0]); + else if (compare < 0) + printf("%s\n%s\n", fn[0], fn[1]); + return 0; +} diff --git a/reverse.c b/reverse.c new file mode 100644 index 0000000..ce1f7c6 --- /dev/null +++ b/reverse.c @@ -0,0 +1,31 @@ +// Julia Tan +// reverse.c + +#include +#include + +void reverse(char* a, char* b, char *c, char *d){ + for (int i = 0; i < (strlen(c) - 1); i ++){ + *b = *a; + a --; // move back 1 position in input string + b ++; // move forward 1 position in reverse string + } +} + +int main (){ + char word[100]; + char *word1 = word; // pointer to entire input string + char* ptr1 = word; // pointer to each letter in input string + printf("Enter a word: \n"); + fgets(word, sizeof(word), stdin); + char revWord[sizeof(word)]; // reverse string + char* ptr2 = revWord; // pointer to each letter in reverse string + char *word2 = revWord; // pointer to entire reverse string + while (*ptr1 != '\0'){ // finds end of string + ptr1 ++; + } + ptr1 -= 2; + reverse(ptr1, ptr2, word1, word2); + printf("The reverse string is:\n%s\n", word2); + return 0; +}