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 @@
Aly Milich

1. ++*p and *++p both increment the value of the array that the pointer is pointing to whereas *p++ moves the pointer over.
Copy link
Copy Markdown
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. Left to right.
Copy link
Copy Markdown
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. Pointers make it easier to use arrays to change the values of certain spots. Since you don't need the for loop, it decreases the algorithmic complexity of your code and takes up less space in the computer & is easier to run.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Right! Pointers are also useful because they allow references to functions and support dynamic memory management!


4.1 char array
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

close! We're looking for the specific data type, which is char*

4.2 Invalid -- "xyz" is not in the array
Copy link
Copy Markdown
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 -- \0 will never equal 0
Copy link
Copy Markdown
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
Copy link
Copy Markdown
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 Address is 0
Copy link
Copy Markdown
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
Copy link
Copy Markdown
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 Address is still 0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The answer is actually 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 Pointer
Copy link
Copy Markdown
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 Invalid
4.10 5
60 changes: 60 additions & 0 deletions pointers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <stdio.h>
#include <string.h>

char cat(char *a, char *b){
char *point = a;

while(*point != '\0'){
point ++;
}

while (*b!= '\0'){
*point = *b;
point ++;
b++;
}


}

char comp(char *c, char *d){

char string1[10];
char string2[10];

*c = string1[10];
*d = string2[10];

int i, length1, length2;

length1 = strlen(string1);
length2 = strlen(string2);


for(i=0; length1<length2; i++){
if(string1[i] == string2[i]){
printf("0");
i++;
}
else{
printf("1");
break;
}
}
}

int main(){
char a[] = "aly ";
char b[] = "milich";


cat(a,b);
printf("%s\n", a);

char c[] = "atm";
char d[] = "atm";

comp(c,d);

return 0;
}
29 changes: 29 additions & 0 deletions reverse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* Reverse a string
@author Aly Milich
*/

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

int reverse(char arr[]){
char *ptr1 = arr;

char *ptr2 = ptr1 + strlen(arr) -1;

int i;

for(i=0; *ptr1!=*ptr2; i++){
printf("%c", *ptr2);
--ptr2;
}
}

int main(){

char string[] = "MILICH\0";

reverse(string);

return 0;

}