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
34 changes: 34 additions & 0 deletions Part2
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Matthew Danielson
7/7/16
Part 2 of Assignment7

1) ++*p increments the value of a pointer, and *p++ increments the address of a pointer. *++p is not valid, as it is incrementing invalidly.
Copy link
Contributor

Choose a reason for hiding this comment

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

`Hmm, not quite: 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 order is guaranteed for operator precedence; without this, many operators become essentially useless, because the logic of the code would not be reliable.
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) Pointers are passed by reference allowing for multiple return types. Additionally, constant pointers can be made; additionally, arrays of pointers allow for non-standard char arrays that are of varying size. They can also be used to return an array of values.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! 👍


4) a[0] = 11
a[1] = 11
a[2] = 12
a[3] = 13
a[4] = 14
a[5] = 15
a[6] = 16
a[7] = 17
a[8] = 18
a[9] = 19
p = 12
char *varname[]
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.

you're right that it's a char array, but you were supposed to write the data type, which would be char *!

4.2 would subtract ascii values, resulting in value 1, or SOH
Copy link
Contributor

Choose a reason for hiding this comment

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

Close! "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 Valid, changes a char
Copy link
Contributor

Choose a reason for hiding this comment

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

You're right that this is valid. The answer is 1. '\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
Contributor

Choose a reason for hiding this comment

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

For this question, you should have evaluated it. *a returns the first element of the array, which is 10.

4.5 Returns the address of the beginning of the array
Copy link
Contributor

@oldclesleycode oldclesleycode Jul 11, 2016

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 Returns the value that said pointer points to
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 Returns the address of 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.

````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 Increments the address of 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.

not quite! You're right that it's char, but 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 Gets the address of the variable
Copy link
Contributor

Choose a reason for hiding this comment

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

Functions in C are actually just pointers to a spot in the program where some code exists. Just like you've been creating pointers to structs, strings, and arrays, you can point a pointer at a function too. The main use for this is to pass "callbacks" to other functions, or to simulate classes and objects. In this exercise we'll do some callbacks, and in the next one we'll make a simple object system.

4.10 Returns an integer length of the space in memory
Copy link
Contributor

Choose a reason for hiding this comment

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

So the answer is actually int(*)(int, char**). All you needed to do was evaluate the data types of the &main. Since & returns the memory address, main's data type would change from int to int*. Since argc and argv are just the parameters, they would remain the same data types. So the final answer is int(*)(int, char**).



48 changes: 48 additions & 0 deletions exercise.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* Matthew Danielson
* 7/8/16
* Pointers Practice
* name: exercise.c
*/
#include <stdio.h>

int strcmp(char[], char[]);
void strct(char[], char[]);

int main(){
char *s1 = {"Hey"};
char *s2 = {"all"};
strct(s1,s2);
return 0;
}

int strcmp(char array1[4], char array2[4]){
int toreturn = 0;
for(int x = 0; x< 20; x++){
if(array1[x] != array2[x]){
if(array1[x] > array2[x])
toreturn = -1;
if(array1[x] < array2[x])
toreturn = 1;
break;
}
return toreturn;
}
}

void strct(char array1[4], char array2[4]){
char array3[sizeof(array1) + sizeof(array2)-2];
int x = 0;
printf("%d\n", (int)sizeof(array1));
for(; array1[x] != '\0';x++){
array3[x] = array1[x];
}
for(int y = 0;y<sizeof(array2); x++){
array3[x] = array2[y];
y++;
}
printf("%s\n", array3);




}
27 changes: 27 additions & 0 deletions reverse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* Matthew Danielson
* 7/7/16
* reverse.c
* Takes a string and prints it in reverse
*/

#include <stdio.h>

int main(){
int length;
char throwaway;
printf("How long will your string be?");
scanf("%d", &length);
length ++;
scanf("%c", &throwaway);
char array[length];
printf("\nPlease enter a string:");
fgets(array, sizeof(array), stdin);
char *start = array;
char *end = array + sizeof(array) + 1;
while(*start != *end){
printf("%c", *end);
*end--;
}
printf("\n");

}