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
23 changes: 23 additions & 0 deletions assignment7.txt
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Perfect answer! 💯


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

Choose a reason for hiding this comment

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

💯


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

Choose a reason for hiding this comment

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

right! they also allow references to functions!


4.
4.1 char * (char * are strings)
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 invalid (if "xyz" is an array, you cannot subtract letters in each element)
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 1 ('/0' is equal to 0. when tested if its equal to 0, it is true and therefore returns 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 (first value in array)
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 10 (first value in array)
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 int * (*p is an int pointer)
Copy link
Contributor

Choose a reason for hiding this comment

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

Close! 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 int ** (this makes a pointer for pointer p)
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 * (dereference of the double pointer 'argv')
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 int * (makes a pointer for the main function)
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 you need to also include the parameters, but good job!

4.10 5 (0 to 5, as \0 is also an element in strings)
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.

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

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;
}
31 changes: 31 additions & 0 deletions reverse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Julia Tan
// reverse.c

#include <stdio.h>
#include <string.h>

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;
}