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
42 changes: 42 additions & 0 deletions Assignment7 Sheqi.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Assignment 7 Sheqi Zhang

1. Explain the difference between ++*p, *p++ and *++p, if there is any.
Copy link
Contributor

Choose a reason for hiding this comment

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

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

Answer:
++*p increments the value at location p points to, then evaluates to incremented value.
*p++ evaluates the value at location p points to, then advances p.
*++p increments the value of p, then evaluate the location ++p points to.

2. Is the left to right or right to left order guaranteed for operator precedence?
Answer:
In most situations, the left to right order is guarenteed, while for operators like "+=","-=", the order is right to left.
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. What is the advantage of using pointers?
Answer:
The pointer provide a way for functions to modify their calling argument, and it supports the dynamic memory allocation. It's more effecient comparing with 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! pointers also allow references to functions!


4.
4.1 "abc" <br>
Copy link
Contributor

Choose a reason for hiding this comment

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

close, it'schar* since it's a string, which is an array of characters.

char
4.2 "xyz"[1] - ’y’ <br>
Invalid. "xyz"[1] is an array, while 'y' is character;
4.3 ’\0’ == 0 <br>
boolean. It is judging if it's wrong.
Copy link
Contributor

Choose a reason for hiding this comment

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

right, but which value? '\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 *a <br>
pointer. Because there is a "*" in front of "a".
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 &a[0] <br>
pointer or array.
Copy link
Contributor

Choose a reason for hiding this comment

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

right, it is a pointer because 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*.

It can be the array in the scanf function, it can also be the pointer of the variable a[0].
4.6 *p <br>
pointer. Because there is a "*" in front of "p".
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 &p <br>
pointer or variable.
Because there is a "&", it may be a pointer. While it can also be a variable in the scanf function.
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 *++argv <br>
pointer. Because there is a "*".
Copy link
Contributor

Choose a reason for hiding this comment

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

right! so the type is char*

4.9 &main <br>
Because there is a "&", it may be a pointer. While it can also be a variable in the scanf function.
Copy link
Contributor

@oldclesleycode oldclesleycode Jul 12, 2016

Choose a reason for hiding this comment

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

right, it's a pointer! so the type ends up being int(*)(int, char**)

4.10 sizeof(str) <br>
Integer. The value is the length of str. 3?
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.




33 changes: 33 additions & 0 deletions Final Project Proposal
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Proposal of Final Project Sheqi

My plan is to write a program that can calculate the frequency of the words appearing in a document.

I want to do this because I think it's useful, and I believe the most important characteristic of computer programming is useful. In addition, I can almost handle it with the things I have learend in the summer program.

Steps:
1. Open document 1 and read it, and judge if it opens successfully at the same time.
2. If open successfully, as well as not reaching the end of the document, then calculate the frequency of a word.
3. Repeat step 2 till the end of the document.
4. Close document 1.
5. Open document 2 and try to record data on it, and judge if it opens successfully at the same time.
6. If open successfully, then record all the words and their frequencies in this document.
7. Close document 2.
8. Make all words fit in a static order (maybe from "a" to "z").
9. Open document 3 and try to record data in it, and judge if it opens successfully at the same time.
10. If open successfully, then record all the words and frequencies in the order in document 3.
11. Close document 3.

How to use loops:
During the process calculating the frequency of one word.
How to use advanced datatypes:
Use structures to combine the words and their frequencies together.
How to use functions:
....
How to use arrays:
In the process of calculating the frequency of one word.
Arrays:
In the structure.

External Library:
I don't understand what is an external library, but I can find external help from my mom. She is a professor in Tsinghua University teaching computer linguistics.

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

int main(){
char string[20];
printf("Please enter a string within 20 characters:\n");
fgets(string,sizeof(string),stdin);
char* begin;
char* end;
char a;
begin=string;
end= begin + strlen(string)-1;

while(begin<end){
a=*end;
*end=*begin;
*begin=a;
begin++;
end--;
}

printf("%s",string);
return 0;
}
39 changes: 39 additions & 0 deletions strings.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>

char* strcpy(char*s1,const char*s2){
assert((s1!=NULL)&&(s2!=NULL));
char*ret=s1;
while((*s1++==*s2++)!='\0');
return ret;
}

char* strcat(char*s1,const char*s2){
char* address=s1;
assert((s1!=NULL)&&(s2!=NULL));
while(*s1)
{
s1++;
}
while(*s1==*s2)
{
NULL;
}
return address;
}

int main(){
char* s1;
const char* s2;
printf("Please type in the first sentence:\n");
scanf("%s",&s1);
printf("Please type in the second sentence:\n");
scanf("%s",&s2);
printf("%s",strcpy(s1,s2));
printf("%s",strcat(s1,s2));
return 0;
}