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

1. ++*p increments the value of the datatype located at pointer p and then points at that location. *p++ points at the location in memory of p and then increments the value there.
Copy link
Contributor

Choose a reason for hiding this comment

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

The expression ++*p has two operators of same precedence, so compiler looks for associativity. Associativity of operators is right to left. Therefore the expression is treated as ++(*p). The expression *p++ is treated as *(p++) as the precedence of postfix ++ is higher than *. The expression *++p has two operators of same precedence, so compiler looks for associativity. Associativity of operators is right to left. Therefore the expression is treated as *(++p).


2. The left to right order is guaranteed for operator precedence.
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. One advantage of pointers is that you can move along a string and point to different parts of it.
Copy link
Contributor

Choose a reason for hiding this comment

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

-Pointers are more efficient in handling arrays and data tables
-They can be used to return multiple values from a function via function arguments
-Pointers permit references to functions
-Pointers allow C to support dynamic memory management
-Pointers reduce length and complexity of programs


4.1 datatype: char array
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 char *

4.2 this is 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 datatype: int. value: 0
Copy link
Contributor

Choose a reason for hiding this comment

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

'\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 datatype: int. value: 10
Copy link
Contributor

Choose a reason for hiding this comment

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

right!

4.5 datatype: int. value: 10
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 datatype: int. value: 12
Copy link
Contributor

Choose a reason for hiding this comment

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

right!

4.7 datatype: address. 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! which means it's just of type int**. 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 datatype: character
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 datatype: address.
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 way to write this is int(*)(int, char**)

4.10 datatype: int. value: 5
Copy link
Contributor

Choose a reason for hiding this comment

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

The standard size is 8 - even though the array is a smaller size, 8 bytes will be allocated regardless.

27 changes: 27 additions & 0 deletions reverse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* Yael Kelmer.
This code takes a user inputted string and uses a function that I created in order to print the string backwards. */

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

void reversal (char userString[]) {
char *userStringStart = userString;
char *userStringEnd = userStringStart + strlen (userString) - 1;
int i;
for (i = 0; *userStringStart != *userStringEnd; i++) {
printf ("%c", *userStringEnd);
userStringEnd--;
}
printf ("%c\n", *userStringEnd);
}



int main ()
{
printf ("Please type a string of letters\n");
char userString [100];
fgets (userString, sizeof(userString), stdin);
userString [strlen (userString) - 1] = '\0';
reversal(userString);
}
73 changes: 73 additions & 0 deletions stringFuncPointers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* Yael Kelmer.
write a program that implements two functions: strcat() and strcmp(). use pointers. write the main function to execute both of these functions on 2 strings. */

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


void stringCat (char string1[], char string2[]) {
int lengthString3 = strlen (string1) + strlen (string2);
char string3 [lengthString3];
char *string3Ptr = string3;
char *string1Ptr = string1;
char *string2Ptr = string2;
int i;
for (i = 0; i < strlen (string1); i++) {
*string3Ptr = *string1Ptr;
string3Ptr++;
string1Ptr++;
}
for (i = 0; i < strlen (string2); i++) {
*string3Ptr = *string2Ptr;
string3Ptr++;
string2Ptr++;
}
*string3Ptr = '\0';
printf("%s\n", string3);
}

int stringCmp (char string1[], char string2[]) {
int shorterLength;
if (strlen (string1) < strlen (string2)) {
shorterLength = strlen (string1);
}
else {
shorterLength = strlen (string2);
}
char *string1Ptr = string1;
char *string2Ptr = string2;
int i;
for (i = 0; i < shorterLength; i++) {
if (*string1Ptr == *string2Ptr) {
string1Ptr++;
string2Ptr++;
}
else if (*string1Ptr < *string2Ptr) {
return 1;
}
else if (*string1Ptr > *string2Ptr) {
return -1;
}
}
if (strlen (string1) == strlen (string2)) {
return 0;
}
else if (strlen (string1) < strlen (string2)) {
return 1;
}
else if (strlen (string1) > strlen (string2)) {
return -1;
}
}



int main() {

char string1 [] = "hello";
char string2 [] = "world";
printf ("This is the concatenation of string1 and string2: ");
stringCat(string1, string2);
int comparison = stringCmp(string1, string2);
printf ("This is the comparison of string1 and string2: %d\n", comparison);
}