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 @@
Olivier Gabison

1. ++*p dereferences the value that the p variable is pointing to and then increments it. While *p++ increments the 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.

Perfect answer! 💯

and then dereferences it. Finally, *++p first increments the pointer and then dereferences it.

2. No, order of precedence is not done through left to right order.
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! 💯


3. I think the best uses of pointers is passing by reference and dynamic starting sizes on arrays.
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! they also allow references to functions and reduce complexity of programs!


4.1 char *, its a string, and char[] has '\0' at the end of it.
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.

💯

4.3 1 - the ASCII value of '\0' is 0. Therefore, 0 = 0, which returns 1;
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.

💯

4.4 int, dereference of pointer a
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! for integer values, you should have evaluated it (so the answer is 10)

4.5 int *, it creates a pointer that is used upon a[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.

right!

4.6 int, dereference of pointer p
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! again, you should have evaluated it (so the answer is 12)

4.7 int **, creates a pointer for pointer p
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.

💯

4.8 char *, derefernce of the double pointer 'argv'
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.

💯

4.9 This does not work as you cannot cast a pointer to a function
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.

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 (only when string.h is included)
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.

It's actually 8 because pointers are allocated 8 bytes regardless of the size!

18 changes: 18 additions & 0 deletions reverse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdio.h>

int main(){

char word[100] = "HELLOHH"; //THIS IS THE WORD THAT WILL BE REVERSED
char *Start = word - 1;
char *End = Start + strlen(word);
int i;
for (i=0; *Start!=*End; i++) {
printf("%c",*End);
End--;
}
printf("\n");



return 0;
}
48 changes: 48 additions & 0 deletions stringf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <stdio.h>

int strcmp(char *a, char *b){
if(*a > *b){
return 1;
} else {
if (*a < *b){
return -1;
} else {
a++;
b++;
return strcmp(a, b);
}
}
}

char *strcat(char *a, char *b){
char *answer[100];
int i;

for(i = 0; i < 100; i++){
if(*a == '\0'){
break;
}
answer[i] = *a;
a++;
}

for (int j = i; i < 100; j++){
if(*b == '\0'){
break;
}
answer[j] = *b;
b++;
}

return answer;
}

int main(){

char s1[] = "Hello";
char s2[] = "Hallo";

printf("%s", strcat(s1,s2));

return 0;
}