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 @@
Jack Rosen
1. ++*p increments the value at point p. *p++ increments p and then returns the value. *++p increments p and then returns the value as well.
Copy link
Copy Markdown
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.

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. It is only guaranteed for certain operators. The first level of operators goes from left to right, the second goes from right to left, and the rest go from left to right.
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.

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.The advantages of using pointers is that they are very powerful and help a person work with arrays. It is also a way to access a variable without having to call for 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.

Pointers are more efficient in handling arrays and data tables and can be used to return multiple values from a function via function arguments. They also allow references to functions and C to support dynamic memory management. Lastly, Pointers reduce length and complexity of programs!

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

💯

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.

Please note that while you got this right, you didn't explain your answers!

4.2 invalid
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.

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

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

Yup! but again, please explain answers as the directions said.

4.5 0
Copy link
Copy Markdown
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 12
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! but again, explain your answers!

4.7 2
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.

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 he memory address is stored - in other words, a pointer to a pointer.

4.8 char
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.

Close, but 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 0
Copy link
Copy Markdown
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 was tricky, but it it's 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 6
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.

sideof(str) is always 8 because all pointers are 8 bytes no matter what they're pointing to.

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

void stringReversal(char *input)
{
char *ptrBegin = input, *ptrEnd = ptrBegin + strlen(input) - 1;
for (; ptrBegin <= ptrEnd; --ptrEnd)
{
if (*ptrEnd == '\n')
{
continue;
}
printf("%c", *ptrEnd);

}

}

int main()
{
printf("Write anything you want!\n");
char input[1000] = {};
int i;
fgets(input, sizeof(input), stdin);
for (i = 0; i < strlen(input); i++)
{
if (input[i] == '\n' || input[i] == '\0')
{
i--;
break;
}
}
stringReversal(input);
printf("\n");
return 0;
}
52 changes: 52 additions & 0 deletions string.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <stdio.h>
#include <string.h>
void concat(char *ptr1, char *ptr2)
{
char *ptr3 = ptr1;
while (*ptr3 != '\0')
{
ptr3++;
}
while (*ptr2 != '\0')
{
*ptr3 = *ptr2;
ptr3++;
ptr2++;
}
ptr3++;
*ptr3 = '\0';
}
int compare(char *ptr1, char *ptr2)
{
int flag=0;

while(*ptr1!='\0' && *ptr2!='\0'){
if(*ptr1 != *ptr2){
flag=1;
break;
}
ptr1++;
ptr2++;
}
if (flag==0 && *ptr1=='\0' && *ptr2=='\0')
{
return 0;
}
else if (*ptr1 > *ptr2)
{
return 1;
}
else if (*ptr2 > *ptr1)
{
return -1;
}

}
int main()
{
char ptr1[12] = "Hello ", ptr2[] = "World";
concat(ptr1, ptr2);
printf("%d\n", compare(ptr1,ptr2));
printf("%s\n", ptr1);
return 0;
}