-
Notifications
You must be signed in to change notification settings - Fork 20
Assignment 7 Lloyd Page #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| Lloyd Page | ||
| 1. ++*p dereferences the value that p points to and increments it. *p++ increments the pointer, and then dereferences it. *++p increments the pointer and then dereferences it. | ||
| 2. No, the operands on the same level as prefix ++ and = read right to left,all other operands read left to right. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right! |
||
| 3. Dynamic starting size arrays, pass by reference for values, Dynamic memory allocation, provide a way to modify fuction calling arguments, more efficient use of memory and time in regards to arrays, multiple return functions, return arrays | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right! 👍 |
||
| 4.1 char *, its a string, which is a char[] with a '\0', and all arrays are pointers of their respective data type | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right! 💯 |
||
| 4.2 Invalid, xyz is not a declared array | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since it's in quotations, it's automatically an array of characters (by definition that is what a string is). 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 1, ASCII value of '\0' is 0, 0==0 returns 1 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 |
||
| 4.4 int, dereference of the pointer a | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would actually evaluate to 10 since *a references the first element of the array. |
||
| 4.5 int *, creates a pointer assigned to a[0] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right!! |
||
| 4.6 int, dereference of the pointer p | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not quite! 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 int **, creates a pointer for pointer p | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right! |
||
| 4.8 char*, derefernce of the double pointer argv | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 |
||
| 4.9 Invalid, can't create a pointer to a function | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 4.10 5 or logic error, if called within the main function, and string.h is included, it will return 5, otherwise it will give you an error due to lack of string.h's inclusion, or logic error if called outside main. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sizeof(str) returns 8. sizeof() always returns an integer if used properly. the size of a string is always 8. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /*Lloyd Page*/ | ||
| /*String reversal function with 2 pointers, user input*/ | ||
| #include<stdio.h> | ||
| void reverse(char*,char*); | ||
| int main() | ||
| { | ||
| char y; | ||
| do | ||
| { | ||
| char handler[100]; | ||
| printf("Enter a string\n"); | ||
| fgets(handler,sizeof(handler),stdin); | ||
| char*p=handler; | ||
| char*x=handler; | ||
| for(int i=0;handler[i]!='\n';i++) | ||
| { | ||
| x++; | ||
| } | ||
| *x='\0'; | ||
| reverse(x,p); | ||
| printf("Run Again?(y/n)"); | ||
| y=getchar(); | ||
| fgets(handler,sizeof(handler),stdin); | ||
| }while(y=='y'||y=='Y'); | ||
| return 0; | ||
| } | ||
| void reverse(char*x,char*p) | ||
| { | ||
| while(x!=(p-1)) | ||
| { | ||
| printf("%c",*(x-1)); | ||
| x--; | ||
| } | ||
| printf("\n"); | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /*Lloyd Page*/ | ||
| /*Implement strcat, strcmp, use pointers to execute functions on 2 strings*/ | ||
| #include<stdio.h> | ||
| int strcmp(char*,char*); | ||
| char* strcat(char*,char*); | ||
| int main() | ||
| { | ||
| char s1[12]="Hello"; | ||
| char s2[]="Hell"; | ||
| int x=strcmp(s1,s2); | ||
| printf("%d\n",x); | ||
| strcat(s1,s2); | ||
| printf("%s\n",s1); | ||
| return 0; | ||
| } | ||
| int strcmp(char*a,char*b) | ||
| { | ||
| if(*a>*b) | ||
| { | ||
| return 1; | ||
| } | ||
| else | ||
| { | ||
| if(*b>*a) | ||
| { | ||
| return -1; | ||
| } | ||
| else | ||
| { | ||
| if(*a=='\0'&&*b=='\0') | ||
| { | ||
| return 0; | ||
| } | ||
| else | ||
| { | ||
| a++; | ||
| b++; | ||
| return strcmp(a,b); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| char* strcat(char*a,char*b) | ||
| { | ||
| int i=0,j=0,l=0; | ||
| while(*(a+i)!='\0') i++; | ||
| while(*(b+j)!='\0') j++; | ||
| int k=i; | ||
| while(l<j) | ||
| { | ||
| *(a+k)=*(b+l); | ||
| k++; | ||
| l++; | ||
| } | ||
| return a; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect! 💯