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


1. ++*p increments what the pointer points to whereas the *p++ actually increases the value of the pointer. *++p is also increases what the pointer points to.
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.

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. Oh the left is guaanteed because its higher in the order of operations.
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 allow easier use with arrays. You can go through the pointer without the for loop and it allows easier access outside of the main function.
Copy link
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 more efficient and can return multiple values from a function.



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

Close! As I said in class today, we're looking for the data type, which is char*.

4.2 Invalid becaus xyz in quotes is not an arry
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 becuse that will never be true
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
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 address is 0
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 pointer
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 adress is 0 becuase we haven't moved it anywhere only increased the value at a point
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 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.

Close! you just forgot to include char.

4.9 Invalid, you can access main like that because it isn't 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.

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.

The format of a function pointer goes like this:
int (*POINTER_NAME)(int a, int b)

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**).

4.10 5
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.

I don't want to edit this now that we're going over it, but I just didn't understand what the questions were asking....
Copy link
Contributor

Choose a reason for hiding this comment

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

Let me know if you understand now!

Copy link
Author

Choose a reason for hiding this comment

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

Thank you so much for the detailed explanations!! They really helped me and I know that probably took so much time. Thank you!!

Emma Ladouceur
Mobile: 917-628-9337
Home: 212-348-0296

On Jul 11, 2016, at 6:12 PM, Lesley Cordero notifications@github.com wrote:

In assignment7.txt:

+3. pointers allow easier use with arrays. You can go through the pointer without the for loop and it allows easier access outside of the main function.
+
+
+4.1 char array
+4.2 Invalid becaus xyz in quotes is not an arry
+4.3 Invalid becuse that will never be true
+4.4 pointer
+4.5 address is 0
+4.6 pointer
+4.7 adress is 0 becuase we haven't moved it anywhere only increased the value at a point
+4.8 A pointer
+4.9 Invalid, you can access main like that because it isn't a pointer
+4.10 5
+I don't want to edit this now that we're going over it, but I just didn't understand what the questions were asking....
Let me know if you understand now!


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

Binary file added point
Binary file not shown.
65 changes: 65 additions & 0 deletions point.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#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 compare(char *c, char *d){
char string1[10];
char string2[10];

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

int i, len1, len2;

len1 = strlen(string1);

len2 = strlen(string2);

for(i=0; len1<=len2; i++){

if(string1[i] == string2[i]){

printf("0");
i++;
}

else{
printf("1");
break;

}


}
}


int main(){
char a[] = "Emma";
char b[] = "Ladouceur";
cat(a, b);
printf("%s\n", a);
char c[]="Emma";
char d[]="Emma";

compare(c,d);
return 0;

}
Binary file added reverse
Binary file not shown.
35 changes: 35 additions & 0 deletions reverse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//Emma Ladouceur


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


int reverse(char arr[]){


char *ptrStart = arr;

char *ptrEnd = ptrStart + strlen(arr)-1;

int i;
for(i=0; *ptrStart != *ptrEnd; i++){
printf("%c", *ptrEnd);
--ptrEnd;

}

}
int main(){
printf("Enter a string of no more than 100 letters\n");
char string[100];
fgets(string, sizeof(string), stdin);
reverse(string);



return 0;



}