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
14 changes: 14 additions & 0 deletions assignment7.txt
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.
Copy link
Contributor

Choose a reason for hiding this comment

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

Perfect! 💯

2. No, the operands on the same level as prefix ++ and = read right to left,all other operands read left to right.
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

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

Right! 💯

4.2 Invalid, xyz is not a declared array
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
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 the pointer a
Copy link
Contributor

Choose a reason for hiding this comment

The 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]
Copy link
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 the pointer p
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! 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
Copy link
Contributor

Choose a reason for hiding this comment

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

Right!

4.8 char*, derefernce of the double pointer argv
Copy link
Contributor

Choose a reason for hiding this comment

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

💯

4.9 Invalid, can't create a pointer to a function
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.

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 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.
Copy link
Contributor

Choose a reason for hiding this comment

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

36 changes: 36 additions & 0 deletions reverse.c
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");
}

56 changes: 56 additions & 0 deletions stringf.c
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;
}