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
24 changes: 24 additions & 0 deletions assignment7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Bettina Benitez

1. ++*p reads right to left. Therefore, it increments point p. Same with *++p, the order of operation is right to left. But point p is incremented then the dereferenced. *p++ is read left to right therefore it is incremented then dereferenced.
Copy link
Contributor

Choose a reason for hiding this comment

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

💯


2. No the order of precedence is not guaranteed
Copy link
Contributor

Choose a reason for hiding this comment

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

💯


3. advantages:
- it points to a specific lovation in memory
- provides direct access to memory
- reduces storage and complexity
Copy link
Contributor

Choose a reason for hiding this comment

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

right! and lastly, it allows references to functions!


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

💯 but give explanations!

- 4.2: invalid
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: invalid
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.4: *int pointer to the array
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 10 because it's just pointing to the first element of the array!

- 4.5: *char reference to the array
Copy link
Contributor

Choose a reason for hiding this comment

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

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

Choose a reason for hiding this comment

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

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: reference to the pointer
Copy link
Contributor

Choose a reason for hiding this comment

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

right, so the answer is 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: incrementing the char argv
Copy link
Contributor

Choose a reason for hiding this comment

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

Close, but not quite! 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: reference to main
Copy link
Contributor

Choose a reason for hiding this comment

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

right, so the answer is int(*)(int, char**)

- 4.10: part of the fgets fucntion to get the string.
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.



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

void getReverse (char *x) {
int i;
int length = strlen(x); //gets the length of the string
char *first = x; //points to the first character of the string
char *last = x + length - 1; //points to the last character (not \0) of the string
char temp; // temp is used as a place holder when switching letters
for (i = 0; i < length/2; i++) { //length is divided by 2 because it only needs to go half way to swap
temp = *first;
*first = *last;
*last = temp;
*first ++;
*last--;
}
printf("%s", x);
}

int main () {
printf("Enter a word: ");
char word [100];
fgets(word, sizeof(word), stdin);
getReverse (word);
}
40 changes: 40 additions & 0 deletions strpoint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//Bettina Benitez
#include <stdio.h>
#include <string.h>

void strCat (char *a, char *b) {
char *p = a;
while (*p != '\0') {
p++;
}
strcat(a, b);
*p--;
*p = ' ';
printf("%s", a);
}

void strCmp(char *a, char *b) {
if (strcmp(a, b) == 0)
printf("Words are the same\n");
else
printf("Words are the same\n");
}

int main () {
char aUserIn[100], bUserIn[100], strOpt[100];
printf("Enter first word: ");
fgets(aUserIn, sizeof(aUserIn), stdin);
printf("Enter second word: ");
fgets(bUserIn, sizeof(bUserIn), stdin);
printf("Choose to concatenate or compare: ");
fgets(strOpt, sizeof(strOpt), stdin);
char con[100] = "concatenate\n";
char com[100] = "compare\n";
if (strcmp(strOpt, con) == 0)
strCat (aUserIn, bUserIn);
else if (strcmp(strOpt, com) == 0)
strCmp(aUserIn, bUserIn);
else
printf("Error: %s is not an option\n", strOpt);
return 0;
}