C Small Problem Collection

Source: Internet
Author: User

Abstract: In the study of C language programming encountered some easy to confuse the problem of error, record down memo.

C Language Learning small problem collection

Wu Long Harry
Date: 2015-11-24
Platform:Window7 64bit,c#: Visual Studio Community, C:TCC 0.9.26 (x86-64 Win64)

Reference:


Chapter:

    • Array element a[n++] is a[n] or a[n+1]?
    • How exactly does 2 recursion work?
    • The number of elements passed in the array?

Body:

One, array element a[n++] is a[n] or a[n+1]?

In the study sort, saw a kind of writing, a[n++], this a[n++] is a[n] or a[n+1]? Needless to say, on the test code:

#include <stdio.h>
int main (void) {
int a[4]={1,2,3,4};

printf ("Array:");
for (int i=0;i<4;i++) {printf ("%d", A[i]);}
printf ("n=%d \ n");

int n=0;
a[n++]=8;

printf ("Array:");
for (int i=0;i<4;i++) {printf ("%d", A[i]);}
printf ("n=%d \ n");
return 0;
}
/* Results:
Array:1 2 3 4 n=0
Array:8 2 3 4 n=1
*/

The result shows: a[n++] still refers to A[n], and then n=n+1.

How does the 2 recursion work?

When learning to sort quickly, there are 2 recursive situations, such as:

QuickSort (a,left,i-1);
QuickSort (A,i+1,right);

How the hell did they run it? On the test code:

#include <stdio.h>
int n=0;
void recursion (int a,int b) {
if (b>3) return;
if (a!=3) printf ("%2d:r:%d%d \ n", n,a,b);
n++;
b++;
Recursion (0,B);
Recursion (1,B);
}
int main (void) {
Recursion (3,0);
return 0;
}
/* Results:
1:r: 0 1
2:r: 0 2
3:r: 0 3
4:r: 1 3
5:r: 1 2
6:r: 0 3
7:r: 1 3
8:r: 1 1
9:r: 0 2
10:r: 0 3
11:r: 1 3
12:r: 1 2
13:r: 0 3
14:r: 1 3
*/

According to the principle of recursion, using the stack to record the entry and exit of the function, I simply think that the function is pressed into the stack, according to the characteristics of the stack LIFO, I use the above symbol to record the process, the two functions are marked as R0,R1. r0-1--represents the 1th recursive function, the B value is 1;r1-2, the 2nd recursive function, the B value is 2, down decomposition:

Step execution and B value stack and B value description
1 r0-1 r1-1 b=1, execute r0-1, push r1-1 into stack
2 r0-2 r1-1, r1-2 into r0,b++ for 2, execute r0-2, push r1-2 into stack
3 r0-3 r1-1, R1-2, r1-3 enter r0,b++ for 3, execute r0-3, push r1-3 into stack
4 r1-3 r1-1, r1-2 enter r0,b++ for 4, trigger exit, return to the stack to take the last r1-3 execution
5 r1-2 r1-1 r1-3 execution, triggering exit, execution r1-2
6 r0-3 r1-1, r1-3 into r1-2,b++ for 3, execute r0-3, push r1-3 into stack
7 r1-3 r1-1 r0-3 trigger exit, remove r1-3 run from stack
8 r1-1 r1-2 Run r1-1, enter r0-2, push r1-2 into the stack
9 r0-2 r1-2, r1-3 run r0-2, enter r0-3, push r1-3 into the stack
R0-3 R1-2, R1-3 run r0-3, trigger exit
R1-3 r1-2 run r1-3, trigger exit
R1-2 R1-3 Run r1-2,b++ is 3, ready to enter r0-3, push r1-3 into the stack
R0-3 r1-3 run r0-3, trigger exit
R1-3 run R1-3, trigger exit, the stack is empty, all end.

The brain aches, so complex, how do these people think out, he only use to know the exit mechanism and calculation process, do not pay attention to the order of execution.

Three, the number of elements passing the array?

In the learning order, we often want to loop, need to know the length of the array (number of elements), within the same function, with
int len=sizeof (a)/sizeof (a[0]);
Can you tell if you can do that after passing the array into the function? On the test code:

#include <stdio.h>
void Test (int a[]) {
printf ("Byref:a%2d, a[0]%d,num%d\n", sizeof (a), sizeof (a[0]), sizeof (a)/sizeof (a[0]));
}
int main (void) {
int a[]={1,2,3,4};
printf ("None:a%2d, a[0]%d,num%d\n", sizeof (a), sizeof (a[0]), sizeof (a)/sizeof (a[0]));
Test (a);
return 0;
}
/* Output:
None:a, A[0] 4,num 4
Byref:a 8, a[0] 4,num 2
*/

Obviously in the function of passing the array in this way is wrong, because the array as a formal parameter is a pointer, showing the size of the pointer, the 64-bit platform pointer is 8 byte. Check out a bunch of data, in addition to using a struct struct to wrap the array, as follows:

struct a{
int count;
int a[];
};

This can be achieved by the count of the array size, but this involves the initialization of the array and other issues, it is very troublesome.


If you are still in the process of learning to meet these small problems, continue to add.
Not to be continued ...

C Small Problem Collection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.