11.3 Embedded C language

day3

1,

int a[]={<!-- -->0,1,2,3,4,5,6,7,8,9},*p=a,i;

Among them, 0<=i<=9, the incorrect reference to the element of array a is:
a[p-a] *( & amp;a[i]) p[i] *(*(a + i))

a[p-a] = a[0] //p-a=? int*-int* =0//0 is the integer 0, (p address-a address "turn to 10")/number of type bytes, p and The prerequisite for a to be operable is that the two pointers are of the same type.
*( & amp;a[i]) = a[i]
p[i]=*(p + 1)
 *(*(a + i)): *(a + i) has already dereferenced the address. *(a + i) is a data value and cannot be dereferenced anymore.

**2. **Enter data for all elements of the array:

int a[10],i=0;
while(i<10)
    scanf("%d", );

Should be filled in:
a + (i + + ) & amp;a[i + 1] a + i & amp;a[ + + i]

3.

int a[10]={1,2,3,4,5,6,7,8,9,10},*p=a;
printf("%d\
",*(p + 2));

The program output is
3 4 1 2

4.

int a[]={<!-- -->2,4,6,8,10},y=1,x,*p;
p= &a[1];
for(x=0;x<3;x + + )
y + =*(p + x);
printf("%d\
",y);

The output of the program is
17 1819 20

p= & amp;a[1];//P stores the first address of element 4
y + =*(p + x);//y = y + *(p + x);
//y=1;x=0;y=5;
//x=1;y=5 + 6=11;
//x=2;y=11 + 8=19;

5.

If there is a definition of int c[4][5], (*cp)[5]; and the statement cp=c;,
Then the correct reference to the c array elements is:
cp + 1 *(cp + 3) *(cp + 1) + 3 *(*cp + 2)√
//c as a pointer type, is the address of the first element in the array, type int [5] *, that is, int (*)[5], assign it to the variable cp, int (*)[5 ] cp=c;
//cp + 1; second element address
//*(cp + 3); cp + 3 is the address of the fourth element, * dereferences it and takes out its value
//*(cp + 1) + 3: cp + 1 is the value of the second element, dereference it, then + 1, array + number (×
//*(*cp + 2): cp is the first address of the array, dereference to get the value of the first element, + 2, same as above

6. If there is a definition: int x,*pb;, then the correct assignment expression is: pb= & amp;x

pb= & amp;x pb=x *pb= & amp;x *pb=*x

7. printf(“%d\
”,NULL); the output result is
The dependent variable is undefined and outputs an indefinite value 0 -1 1

The first character in ASCII code and Unicode code is NULL, and its value is 0.
C language uses it as the last character of the string to indicate that the string ends here. Invisible characters are usually represented by backslash \’ + their ASCII code value, so NULL is often represented by “\0”. In addition, in the C language header file stdio.h, NULL is defined as 0 by the macro.

8,

void sub(int x,int y,int *z)
{<!-- -->
*z=y-x;
}

main()
{<!-- -->
int a,b,c;
sub(10,5, & amp;a);//x=10,y=5,z= & amp;a;*z=*( & amp;a)=5-10=-5=a;
sub(7,a, & amp;b);//x=7,y=-5,z= & amp;b;*z=*( & amp;b)=-5-7=-12=b ;
sub(a,b, & amp;c);//x=-5,y=-12,z= & amp;c;*z=*( & amp;c)=-12-(-5)= -7
printf("%d,%d,%d\
",a,b,c);
}

The program output results are: -5, -12, -7
5,2,3-5,-12,-7 -5,-12,-17 5,-2,-7

9,

main()
{<!-- -->
int k=2,m=4,n=6,*pk= & amp;k,*pm= & amp;m,*p;
n=k*(m); //
    printf("%d\
",n);
}

The program output is
4 6 8 10

10.

void prtv(int *x)
{<!-- -->
printf("%d\
", + + *x);//x= & amp;a; + + *( & amp;a)= + + a=26// + + *xSame level single eye, Whoever is closest will be counted first.
    //*x + + are equally close, first + +, then *
}
main()
{<!-- -->
int a=25;
prtv( & amp;a);
}

The program output is: 26
23 24 25 26

11,

void sub(double x,double *y,double *z) //x=6.5,y= & amp;a,z= & amp;b
*y=*y-1.0; //*( & amp;a)=*( & amp;a)-1.0;a=a-1.0=1.5
*z=*z + x; //*( & amp;a)=*( & amp;a) + 6.5;a=a + 6.5=1.5 + 6.5=8
}
main()
{<!-- -->
double a=2.5,b=9.0,*pa,*pb;
pa= & amp;a; pb= & amp;b;
sub(b-a,pa,pa);//sub(6.5, & amp;a, & amp;b)× sub(6.5, & amp;a, & amp;a)
    printf("%f\
",a);
}

The output of the program is: 8.000000
9.000000 1.500000 8.000000 10.500000

12. If there is a definition statement: char a=\72’;, then variable a:
Contains 1 character Contains 2 characters Contains 3 characters The definition is illegal

Octal 72 corresponds to decimal 58 and the ASCII value is ":"

13. In C language, the following illegal character constants are
\xff’ \65’ & amp;’ \028’

The following three are all escape characters. The forms '\o', '\oo' and '\ooo' represent the ASCII characters corresponding to the octal code. o represents an octal number; the beginning of '\x' represents the same as This hexadecimal code corresponds to the ASCII character
'\xff' :\x starts with hexadecimal
'\65': octal
'\028': If it does not start with

14. In C language, the following illegal string constants are: y=’
“\121” y=’ “\
\
” “ABCD\x6d”

The value inside

'' can only be a single character

**15. **The simplest and correct expression to determine whether the char type variable c is an uppercase letter is
‘A’<=c<='Z' (c>=’A’)||(c<='Z') ('A'<=c)AND('Z'>=c) ( c>=’A’) & amp; & amp;(c<='Z')

16. char x=A’;
x=(x>=A’ & amp; & amp;x<=Z’)?(x + 32):x;
printf(“%c\
”,x);
The output of the program is: a
Aa Z z

The ASCII code of uppercase letters + 32 is the corresponding lowercase letter
Another: The ASCII code of '' is 32

17. The following statement group that can correctly perform string assignment and initial value assignment is
char s[5]={‘a’,’e’,’i’,’o’,’u’}; *char s;s=”good!”;< /em> char s[5]=“good!”; char s[5];s=“good”;

char s[5]={<!-- -->'a','e','i','o','u'}; //The string ends with '\0' , there is no problem with this array as a character array, but it cannot become a string.
char *s;s="good!";//The string address here is stored in the pointer variable s, which is a character pointer. And instead of directly assigning the value of the string to the pointer variable s, the string is saved in the constant area in the memory, and then its address is assigned to the pointer variable. Since the string is stored in the constant area, the value of the string cannot be modified through *s[i].
char s[5]="good!"; //s is a character array with a length of 5, but there are 6 characters in the following string
char s[5];s="good"; //There are several ways to assign values to arrays: 1) Direct assignment when defining 2) Partial assignment when defining 3) Define first and then assign values one by one; you cannot define first and then assign values later One-time assignment. So s="good"; or s[5]="good"; are both incorrect. And the array name is an address constant and its value cannot be changed.
After an array is declared uninitialized, it can only be assigned through subscripts.

18.

With definition: char str[]="ABCD",*p=str; then the output result of the statement printf("%d\
",*(p + 4)); is

68 0 Address of character D Uncertain value

*p=str;//str is a pointer type, assign str (that is, the first address of the first element in the array) to the pointer variable p
*(p + 4) //p + 4=p[4], which is the address of the fifth element in the array. The fifth element is the character '\0', which is output as 0 in %d format.

19.

The output result of the statement printf("%d\
",strlen("ATS\
012\1")); is

11 109 8

strlen("ATS\
012\1");
// \
 represents a newline character, occupying one byte
//\1 represents the ASCII code character corresponding to an octal number, occupying one byte
// \ means \, which means that '' means '' to prevent it from escaping. If you only write one \,strlen("ATS\
012\1"); then \ will be combined with the following " , although it seems to make sense that " is to prevent " from being escaped, but there is one less " in the brackets, and there is a syntax error.
"ATS\
012\1"
 123 4567 8 9

20,

void fun(char *w,int m)//Parameters (w: array first address, m: array length)
{<!-- -->
char s,*p1,*p2;
p1=w;//array first address A
    p2=w + m-1;//G address in array
while(p1<p2)
{<!-- -->
s=*p1 + + ;
        *p1=*p2--;
        *p2=s;
}
}

//p1:A position B position C position
//*p1: A
//s: A
//p2:G position F position
//*p2: A(G)

main()
{<!-- -->
char a[]="ABCDEFG";
fun(a,strlen(a));
puts(a);
}

The output after running the program is
GFEDCBA AGADAGA AGAAGAG GAGGAGA

21.There is a definition statement: char * aa[2]={“abcd”,”ABCD”}; //Character pointer array, which stores the address of the string
Then the following statement is correct
The values of the aa array elements are the strings “abcd” “ABCD” //The values are two pointers
aa is a pointer variable, which points to a character-type one-dimensional array containing two array elements // pointer-type one-dimensional array
The two elements of the aa array respectively store the first address (five characters) of a one-dimensional array containing four characters.
The two elements of the aa array store the addresses of a’ and A’ respectively. .

& pos_id=img-3NaR5ICn-1699361901255″ alt=”The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly”>

char * arr[3];//arr[3] is an array, which stores an array of pointers.
char (*arr)[3];//arr is a pointer, an array pointer pointing to an array

Both of these do the +1 operation
    char * arr[3]; arr + 1==>char ** + 1 ==>Skip a char *==》The address of arr[1] arr is the address of the first element in the array, the first element is of type char *, so arr is of type char **
    char (*arr)[3]; arr + 1==>char(*)[3] + 1==>Skip 1 char [3]

**22. **If the variable has been correctly defined, when executing the following while statement, after entering the character A, the value of ch is: 0
while(ch=getchar()==A’);

getchar() inputs a character and returns its corresponding ASCII code
    Enter A and press Enter,
1. getchar() first gets A, getchar()=='A' is true, and the result is 1, then ch=1. At this time, the condition in while is true, and the loop continues to execute the condition judgment. After entering A, press return Car, getchar()=='A' is false, the result is 0, ch=0, the condition in while is not true, exit the loop, so ch=0
2. Note that the while loop will only break out of the loop when the condition is not true.

23. The function of the following fun function is to reverse the contents of a string. Please fill in the blanks.

#include <stdio.h>
#include <string.h>

void fun(char str[5])//The 5 in str[5] is useless and the length cannot be limited.
{<!-- -->
int i,j,k;//Calculate the length of the string except '\0' through strlen(), and then -1 to get the subscript of the last character
for(i=0, j=strlen(str)-1 ;i<j;i + + , j--)
{<!-- -->
k=str[i];//i=0, the first character of str[0] string, saved in k
str[i]=str[j];//j=4,str[3]The last character of the string is assigned to str[0]
str[j]=k;//str[0] saved in k is then assigned to str[3]
}
}

24. Write a function to perform the following operations on an array with 10 integers: starting from the element with subscript n to the last element, move forward one position in sequence. Output the result of the move

 1 #include <stdio.h>
  2 
  3 void move(int arr[],int num)
  4 {<!-- -->
  5 int i;
  6 for(i=num;i<10;i + + )
  7 arr[i-1] = arr[i];
  8 
  9 return;
 10}
 11
 12 int main(void){<!-- -->
 13 int arr[10]={<!-- -->1,2,3,4,5,6,7,8,9,10};
 14 int num,i;
 15 scanf("%d", & amp;num);
 16 move(arr,num);
 17 for(i=0;i<10;i + + )
 18 printf("%d ",arr[i]);
 19 printf("\
");
 20 return 0;
 twenty one }
fun(int a[],int n){<!-- -->
    int i;
for(i=n;i<9;i + + )
        a=a[i + 1];
}

main(){<!-- -->
int a[10]={<!-- -->0,1,2,3,4,5,6,7,8,9},n,i;
    scanf(“%d”, & amp;n);
    fun(a,n);
for(i=0;i<9;i + + )//Why i<9?
        printf(“%d ”,a);
}

25. Write a function to sort the letters in the character array in alphabetical order from large to small.

 1 #include<stdio.h>
  2 #define NUM 6
  3
  4 void sort(char *a,int n)
  5 {<!-- -->
  6 int i,j,p,t;
  7 for(j=0;j<n-1;j + + )
  8 {<!-- -->
  9 p=j;//The subscript used to save the maximum value of the current round
 10 for(i=j + 1;i<n;i + + )
 11 {<!-- -->
 12 if(a[p]<a[i])
 13 {<!-- -->
 14 p = i;
 15}
 16
 17}
 18 if(p != j)
 19 {<!-- -->
 20 t = a[j];
 21 a[j] = a[p];
 22 a[p]=t;
 twenty three         } 
 twenty four     }
 25}
 26 void arrout(char *w,int m)
 27 {<!-- -->
 28 int i;
 29 for(i=0;i<m;i + + )
 30 {<!-- -->
 31 printf("%c ",w[i]);
 32
 33}
 34 printf("\
");
 35
 36}
 37 int main(void){<!-- -->
 38 char arr[NUM]={<!-- -->'a','b','c','d','e','f'};
 39 arrout(arr,NUM);
 40 sort(arr,NUM);
 41 arrout(arr,NUM);
 42 return 0;
 43}

26. Write a function to return all the odd numbers in the array in another array.

 1 #include<stdio.h>
  2 #define NUM 6
  3
  4 int jishu(int *w,int *b,int m)
  5 {<!-- -->
  6 int i,n,j=0;
  7 for(i=0;i<m;i + + )
  8 {<!-- -->
  9 if(w[i]%2 != 0){<!-- -->
 10 b[j]=w[i];
 11 j + + ;
 12}
 13}
 14 return j;
 15
 16}
 17 void arrout(int *w,int m)
 18 {<!-- -->
 19 int i;
 20 for(i=0;i<m;i + + )
 21 {<!-- -->
 22 printf("%d ",w[i]);
 twenty three 
 twenty four     }
 25 printf("\
");
 26
 27}
 28 int main(void){<!-- -->
 29 int arr[NUM]={<!-- -->1,2,3,4,5,6},brr[NUM]={<!-- -->};
 30 int num = jishu(arr,brr,NUM);
 31 arrout(brr,num);
 32 return 0;
 33}

27. Please write a function whose function is to find the sum and difference of the two floating point numbers passed in,and send them back to the calling function through formal parameters.

 1 #include<stdio.h>
  2 void fun(double a,double b,double *x,double *y)
  3 {<!-- -->
  4*x = a + b;
  5*y = a - b;
  6}
  7
  8 int main(void){<!-- -->
  9 double a,b,x,y;
 10 scanf("%lf %lf", & amp;a, & amp;b);//Input two floating point numbers
 11 fun(a,b, & amp;x, & amp;y);// & amp;x, & amp;y is used to save the address of the two results
 12 printf("%f %f\
",x,y);
 13 return 0;
 14}

28. Please write a function to select the maximum and minimum numbers from the three numbers transmitted, and pass them back to the calling function through formal parameters.

 2 #include<stdio.h>
  3 void fun(double a,double b,double c,double *max,double *min)
  4 {<!-- -->
  5 *max = a > b?(a>c?a:c):(b>c?b:c);
  6 *min = a < b?(a<c?a:c):(b<c?b:c);
  7}
  8 
  9 int main(void){<!-- -->
 10 double a,b,c,max,min;
 11 scanf("%lf %lf %lf", & amp;a, & amp;b, & amp;c);
 12 fun(a,b,c, & amp;max, & amp;min);
 13 printf("max=%f min=%f\
",max,min);
 14 return 0;
 15}

29. Please write a program to input a line of characters, output each character and its corresponding ASCII code value, and output three pairs per line.

 1 #include <stdio.h>
  2 
  3 int main(void){<!-- -->
  4 char ch;
  5 int i = 0;
  6 while((ch=getchar())!='\
'){<!-- -->
  7 printf("%c--%d ",ch,ch);
  8i++;
  9 if(i%3==0)
 10 printf("\
");
 11 }
 12 printf("\
");
 13 return 0;
 14}

30. Please write a program to count the number of input lines and end the input with !! The row where the number is located is not included in the number of rows

 1 #include <stdio.h>
  2 int main(void)
  3 {<!-- -->
  4 char x;
  5 int k = 0;
  6 while((x=getchar()) != '!')
  7 {<!-- -->
  8 if(x == '\
')
  9k++;
 10}
 11 printf("%d line\
",k);
 12 return 0;
 13}

31. Please write a program to count the number of lowercase letters in an input line.

 1 #include<stdio.h>
  2 #include<ctype.h>
  3 int main(void){<!-- -->
  4 char ch;
  5 int n=0;
  6 while((ch = getchar())!='\
'){<!-- -->
  7 if(islower(ch)){<!-- -->
  8 n + + ;
  9         }
 10}
 11 printf("Number of lowercase letters: %d\
",n);
 12
 13 return 0;
 14}

32. Please write a program to output the following pattern. The number of lines of the pattern is determined by the input integer value.
A
BBB
CCCCC
DDDDDDD
EEEEEEEEE

#include <stdio.h>

int main(void)
{<!-- -->
int i, j, n;
printf("Please enter the number of lines of the pattern:");
scanf("%d", & amp;n);

for (i = 1; i <= n; i + + )
{<!-- -->
for (j = 0; j < n-i; j + + )
{<!-- -->
printf("%c", '\0');
}
for (j = 0; j < 2*i-1; j + + )
{<!-- -->
printf("%c", 64 + i);
}
printf("%c", '\
');
}
return 0;
}

33. Please write a program to output the following pattern.
A
ABA
ABCBA
ABCDCBA

34. Please write the functions mygets and myputs. Their functions are the same as gets and puts respectively. Getchar and putchar are used to read in and output characters in the functions.

getchar() does not read data directly from the keyboard. When the user inputs data, getchar() is on standby. These data will be temporarily stored in the buffer first. When the user enters "\
", that is, when the Enter key is pressed, getchar() starts to read data from the buffer. Only one character is read, and getchar() can read it. "\
".
    After the user enters a string and presses the Enter key, getchar starts working. It reads the first character of the string the user just entered from the buffer.
    An application of getchar() function to make up for the problems of scanf().
    When the user presses the Enter key and the scanf() function receives "\
" as a signal and begins to receive the entire string, it does not receive "\
" itself, and the character remains in the cache. From this, a new usage of the getchar() function can be extended: cleaning up extra characters in the buffer. The program will use the getchar() function to continuously read characters in the buffer into tmp until there are no characters in the buffer.

img

getchar() reads characters one by one
scanf("%s",arr);//You can directly enter a string to assign a value to the array. Therefore the array contains the last element of the string '\0'i
 1 #include <stdio.h>
  2 #include <string.h>
  3 void mygets(char *s){<!-- -->
  4 char c;//Save the characters read each time
  5 //Read characters through the getchar function
  6 //Because getchar only reads one character at a time, this operation needs to be looped until the user presses Enter >. Using a while loop, when the user enters data and presses the Enter key, the data is saved in the buffer, and getchar will
    Continuously read characters in the buffer into tmp until '\
' is encountered and exit the loop. (getchar reads from the buffer to
    After one character, the character is cleared from the buffer)
  7 while((c=getchar())!='\
')
  8 *s + + = c;
  9 
 10 //After reading all, add '\0' to the string
 11 *s = '\0';
 12}
 13 void myputs(const char *s){<!-- -->
 14 //Output through putchar. The parameters of the function are the elements of the character array. Starting from the first element, as long as > the character to be output is not 0 (0 means that the end of the string '\0' has been reached, and the result is 0 , if false, the loop ends at this time > loop), and the loop will continue until the condition is met until '\0' is encountered to end the loop.
 15 while(putchar(*s + + ) != '\0');
 16
 17}
 18 int main(void){<!-- -->
 19 char arr[50]={<!-- -->0};
 20 mygets(arr);//Pass the array address to the mygets function, which is used to enter characters and save them in the array.
 21 printf("myputs:\
");
 22 myputs(arr);//Pass the array of saved characters into the myputs function to output the characters
 23 myputs("\
");
 24 printf("arr_len:%lu\
",strlen(arr));
 25 printf("arr:%s\
",arr);
 26 return 0;
 27}

35. Please write a function to determine whether a string is a palindrome. If it is a palindrome, the function returns a value of 1; otherwise, the return value is 0. A palindrome is a string that is the same when read forward and backward.

 1 #include <stdio.h>
  2 #include<string.h>
  3 int huiwen(char *s){<!-- -->
  4 int i,j;//i is the first actual element subscript in the string, j is the last one
  5 for(i=0,j=strlen(s)-1;i<j;i + + ,j--){<!-- -->
  6 if(s[i]==s[j])
  7 return 1;
  8 else
  9 return 0;
 10}
 11
 12}
 13
 14 int main(void){<!-- -->
 15 char s[80]={<!-- -->0};
 16 scanf("%s",s);
 17 int res = huiwen(s);
 18 printf("%d\
",res);
 19
 20 return 0;
 twenty one }

36. Please write a function to delete the characters at the specified position (subscript) in the string. If the deletion is successful, the function returns the deleted character; otherwise, it returns a null value.

 1 #include<stdio.h>
  2 #include<string.h>
  3 char delete(char *arr,int num)
  4 {<!-- -->
  5 int i;
  6 char ch = arr[num];
  7 if(num > strlen(arr)-1||num < 0)
  8 return 0;
  9 for(i = num;arr[i];i + + ){<!-- -->
 10 arr[i]=arr[i + 1];
 11 return ch;
 12}
 13}
 14
 15 int main(void)
 16 {<!-- -->
 17 int num;
 18 char arr[]="helloworld";
 19 scanf("%d", & amp;num);
 20 char del = delete(arr,num);
 21 if(del == 0)
 22 printf("%d\
",del);
 23 else
 24 printf("%c\
",del);
 25
 26 return 0;
 27 }

ppt_day3

1. If there is the following definition:

int grid[30][100];
a. Use one method to express the address of grid[22][56]. //*(grid + 22) + 56
b. Use 2 methods to express the address of grid[22][0]. //1)*(grid + 22) + 0 2)0[22[grid]]
c. Use 3 methods to express the address of grid[0][0].
    //1)*(grid + 0) + 0 2)0[0[grid]] 3)0 + *(0 + grid)

3. Use variable a to give the following definition

a) an integer int a

b) A pointer to an integer int * a

c) A pointer to a pointer, the pointer it points to is an integer type int **

d) An array of 10 integers int a[10]

e) An array with 10 pointers, the pointer is int * a[10] pointing to an integer number

f) A pointer to an array of 10 integers int (*p)[10]

g) A pointer to a function that takes an integer parameter and returns an integer

h) An array of 10 pointers pointing to a function that takes an integer parameter and returns an integer

8. Under the x86 platform, analyze the output results of the following code:

#include <stdio.h>
int main(void)
{<!-- -->
int a[4] = {<!-- -->1, 2, 3, 4};
int *ptr1=(int *)( & amp;a + 1);
int *ptr2=(int *)((int)a + 1);
printf("%x, %x\
", ptr1[-1], *ptr2);
return 0;
}
syntaxbug.com © 2021 All Rights Reserved.