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

1. For ++*p, it is read right to left. For *p++, it first adds, then does the *p. For *++p, it also reads right to left.
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. Yes, left to right or right to left is guarenteed because the compiler uses a sort of "order of operations" in it's own sense. For terms that are the same in terms of precedence, the compiler reads right to left.
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. The advantage of using pointers is that a) You can have a string with an undefined length. b) Pass variables by reference rather than value.
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.1 char a[3] = "abc"; for a string
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 were looking for the data type, which is char*

4.2 Invalid because you are trying to declare an array with a string, then using a - instead 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.

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 because you are trying to set a character from a string(end of string character) to an integer
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 Pointer- int *a = variable; Points to the variable address
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 Invalid because arrays already pass by refernce
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 Pointer- int *p = variable; Points to the variable address
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 Refernce- scanf("%d", &variable); You are giving the function the address of the variable so it can be used in a 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, but we're looking for 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; increases the value that argv is pointing to by 1 after the operation is complete.
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- fxn(&main); you are sending the address of the main variable to the function so that it can be used as a 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! the actual answer is int(*)(int, char**) since we're looking for the data type

4.10 sizeof(str); used to find the size of a string in terms of bytes.
Copy link
Contributor

Choose a reason for hiding this comment

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

right! which is just 8 because that's the standard size!

31 changes: 31 additions & 0 deletions reverse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*Sean Kee*/
/*Reverse inputted string using pointers*/
#include <stdio.h>
#include <string.h>

void stringReverse(char string[]) {
char swap;
char *beginptr = &string[0];
char *endptr = &string[strlen(string) - 1];

while (beginptr < endptr) {
swap = *beginptr;
*beginptr = *endptr;
*endptr = swap;
beginptr++;
endptr--;
}

printf("New String: %s\n", string);

}

int main() {
char input[100];
printf("Input a string\n#: ");
fgets(input, sizeof(input), stdin);
input[strlen(input)-1] = '\0';
stringReverse(input);

return 0;
}
62 changes: 62 additions & 0 deletions strfxn.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*Sean Kee*/
/*String Functions*/

#include <stdio.h>

void stringcat(char *str1, char *str2) {
while (*str1 != '\n') {
str1++;
}
while (*str2 != '\n') {
*str1 = *str2;
str2++;
str1++;
}
*str1 = '\0';

}

int stringcmp(char *str1, char *str2) {
while (*str1 != '\n' || *str2 != '\n') {
if (*str1 == *str2) {
str1++;
str2++;
}
else
return 1;
}
return 0;
}

int main() {
int menu = 1;
int option;
char input1[100];
char input2[100];
int returned;
printf("Input String 1\n#: ");
fgets(input1, sizeof(input1), stdin);
printf("Input String 2\n#: ");
fgets(input2, sizeof(input2), stdin);

while (menu == 1) {
printf("\nWhat would you like to do?\n1: Concatenate Strings\n2: Compare Strings\n3: Exit\n#: ");
scanf("%d", &option);
switch(option) {
case 1:
stringcat(input1, input2);
printf("\nNew String: %s\n", input1);
break;
case 2:
returned = stringcmp(&input1, &input2);
if (returned == 0)
printf("\nIdentical Strings\n");
else
printf("\nNot Identical Strings\n");
break;
case 3:
menu = 0;
break;
}
}
}