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
30 changes: 30 additions & 0 deletions homework7
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Caleb Gershengorn

1:
++*p:advances the pointer one space in the array in memory, than checks what is in that spot.
*p++: checks the space in memory at that spot, then advances one space.
*++p" does the same as the first, because the placement of the '++' makes the difference, not the '*'
Copy link
Contributor

Choose a reason for hiding this comment

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

💯


2:
Left to right garuntees operator precedence.
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:
Using pointers allows you to more easily look through lists and access things in memory than having to reference them directly.
Copy link
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


4:
int *p=12
*p=2
char

Copy link
Contributor

Choose a reason for hiding this comment

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

you didn't evaluate these questions!

4.1--valid
4.2--invalid(dash is not a thing)
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--valid(just false)
Copy link
Contributor

Choose a reason for hiding this comment

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

'\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--valid(dereference operator)
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--valid(stores a value at 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.

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--valid(dereference operator)
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--valid(stores value at p)
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--invalid
Copy link
Contributor

Choose a reason for hiding this comment

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

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--invalid(main is a special use function)
Copy link
Contributor

Choose a reason for hiding this comment

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

you can have pointers to a function.

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


Copy link
Contributor

Choose a reason for hiding this comment

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

They're all valid!


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

void swap(char*word,int sizeWord){
int i;
int j;
char * wordPtr=word;
if (sizeWord%2==0)
{
for (i=0;i<sizeWord/2;i++)
{
char tmp = wordPtr[i];
wordPtr[i] = wordPtr[sizeWord-i];
wordPtr[sizeWord-i] = tmp;
}
}
else
{
for (i=0;i<(sizeWord-1)/2;i++)
{
char tmp = wordPtr[i];
wordPtr[i] = wordPtr[sizeWord-i];
wordPtr[sizeWord-i]=tmp;
}
}
for (j=0;j<sizeWord;j++)
{
printf("%c\n",wordPtr[j]);
}
}

int main(){
char* arr1;
printf("Enter the word you want to flip: \n");
fgets(arr1,sizeof(arr1),stdin);
swap(arr1,sizeof(arr1));
}