大连东软信息学院C语言指针练习.docx_大连东软信息学院
大连东软信息学院C语言指针练习.docx由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“大连东软信息学院”。
一、Make a choice 1.For the same data type pointer variable, which operator can notused in C program.A.+
B.-
C.=
D.== 2.when0
B.a[p-a]
C.p+i
D.*(&a[i])3.when0
p=a;
A.&(a+1)
B.a++
C.&p
D.&p[i] 4.What is the result for the following program? #include main(){ int a[]={1,2,3,4,5,6},*p;
p=a;
*(p+3)+=2;printf(“%d,%dn”,*p,*(p+3));}
A.0,5
B.1,5
C.0,6
D.1,6 5.what is the result for the following program? #include main(){ int a[12]={1,2,3,4,5,6,7,8,9,10,11,12}, *p[4],i;for(i=0;i
A.输出项不合法
B.6
C.8
D.12 6.For the following statements, what’s the value of(p1-p2)? int a[10], *p1, *p2;p1=a;p2=&a[5];
A.5
B.6
C.10
D.没有指针与指针的减法
7.for this function prototype: void adder(int *ptr,int b);if there are two integers: int op1=2;int op2=4;which statement of function call is right?
A:adder(*op1,op2);B:adder(op1,op2);C:adder(&op1,op2);D:adder(&op1,&op2);8.What does the following program outputs to screen_____ voidToSrn(int *);main(){ int a=8;int *ptr=&a;ToSrn(ptr);} voidToSrn(int *ptr){ printf(“%p”,&a);} A:compile error,can not be run B:8 C:16 D:addre of a 9.Auming int *p,a=4;p=&a;which of the following statements is all means addre A:a,p+1 B:&a,*p C:&a,p D:*a,p 10.Auming inta[10],*p;which one is right A:*p=&a[0];B:*p=a;C:p=a[0];D:p=&a[0];11.Auming :int a[10],*p=a;which one is the addre of a[9] A:a[0]+9 B:&(p+9)C:*(p+9)D:a+9 12.Auming:
char s1[]=“Hello”,s2[10],*s3=“HelloWorld”,*s4;which one of the following statements is correct? A:strcpy(s1[0],“Morning”);B:s2=“Morning”;C:strcpy(s3,“Morning”);D:strcpy(s4,“Morning”);13.For the following statements,after execution the statement a=p+2;what’s the value of a[0]? float a[3]={1.2,45.6,-23.0};float *p=a;
A.1.2
B.45.6
C.-23.0
D.语句有错
14.What format specifiers(in order)should be used in the printf()statement in thefollowing program? Note that in the program the correct format specifiers have beenreplaced by Z.#include int main(void){ int x = 5;int *x_ptr = &x;printf(“%Z, %Z, %Z, %Zn”, x, *x_ptr, &x, x_ptr);}(a)%f, %p, %d, %p(b)%d, %d, %p, %p(c)%d, %p, %d, %p(d)%p, %d, %d, %p
二、Fill the blank.1.the result of printf(“%sd”,“ab cd”);is __【1】___。2.A pointer is a special type of variable which stores the
【1】
of a memory location.3.auming:int a[]={1,3,5,7,9,11},*p=a;and the value of *p is _【1】__.the value of *(a+5)is __【2】
4.For the following statement, int a[]={8,1,2,5,0,4,7,6,3,9};What’s the value of a[*(a+a[3])]?._____________
二、read the following programs.1.On a machine in which addrees are 4 bytes, what is printed by the following program: #include int main(void){ char fun[] = “Programming is fun.”;char favorite[] = “My favorite cla is programming.”;char *x = fun;printf(“%dn”, sizeof(fun));printf(“%dn”, sizeof(favorite));printf(“%dn”, sizeof(x));}
2.What does the following program print? #include int main(void){ int data[] = {1, 2, 3, 4, 5, 6, 7};int *ptr = data;int i;printf(“ i *ptrn--------n”);for(i = 2;i >-4;i--){ printf(“%2d, %2dn”, i, *ptr);ptr+=i;} }
三、write the following program.1.Rewrite the following program such that the function has a return type of void andthe variable y gets its value using pointers.#include intdbl(intnum);int main(void){ int x = 13;x = dbl(x);printf(“x doubled is %dn”, x);} intdbl(intnum){ return 2*num;}
2.Write a program that has a function that when paed a string will print every fourthcharacter of the string.In main()you should create the string ”This is an examplestring.” and pa this to your function to be printed.3.Change the program below to print I love programming.You should do this byusing the values in lovetext to change hatetext.Hint: think about the relationshipbetween the index values of the letters in love and the index values for the word hate.This can be done without creating any new variables.#include int main(void){ int i;charhatetext[] = “I hate programming.”;charlovetext[] = “love”;/* Your code goes here.*/ printf(“%sn”, hatetext);}
4.Write a program that has a function that when paed a string will print every fourthcharacter of the string.In main()you should create the string ”This is an examplestring.” and pa this to your function to be printed./*Void printString(char *p);*/ Void printString(char *p){ Int i=0,len=0,length=strlen(p);While(*p!=NULL&&len
{ Printf(“%c”,*p);P+=4;Len+=4;} } main(){ Char str[]=”This is an examplestring.”;printString(str);} 5./* Write a program.In main(), declare an integer array,and initialize the array through keyboard.Create a multiply()function that when given the array and the number of array elements,in which can double the value of the array elements(that is a[i]*2).Also create another print()function for printing the array elements.Call the two functions in main function.Tip: when running the program, input n with 14.*/
#include
#define N 14
void multiply(int a[],int n);void print(int *p);
main(void){ int a[N],i;
for(i=0;i
{ printf(“n No.%d: ”,i+1);scanf(“%d”,&a[i]);
}
/*(1)call the multiply()function*/
/**********Program**********/ multiply(a,N);
/********** End **********/
/*(2)call the print()function*/
/**********Program**********/ Print(a);
/********** End **********/
}
void multiply(int a[],int n){ int I;
/*(3)double the value of the array elements*/
/**********Program**********/
/********** End ***********/
} void print(int *p){ int i;
printf(“nThe output values after multiply are:n”);
/*(4)output the value of the array elements*/
/**********Program**********/
/********** End **********/
}
1.Write a program that has a function that when paed a string will print every fourthcharacter of the string.In main()you should create the string ”This is an examplestring.” and pa this to your function to be printed./*Void printString(char *p);*/ Void printString(char *p){ Int i=0,len=0,length=strlen(p);While(*p!=NULL&&len
{ Printf(“%c”,*p);P+=4;Len+=4;} } main(){ Char str[]=”This is an examplestring.”;printString(str);} 2./* Write a program.In main(), declare an integer array,and the array
initialize through keyboard.Create a multiply()function that when given the array and the number of array elements, in which can double the value of the array elements(that is a[i]*2).Also create another print()function for printing the array elements.Call the two functions in main function.Tip: when running the program, input n with 14.*/
#include
#define N 14
void multiply(int a[],int n);void print(int *p);main(void){
for(i=0;i
{ printf(“n No.%d: ”,i+1);scanf(“%d”,&a[i]);int a[N],i;
}
/*(1)call the multiply()function*/
/**********Program**********/ multiply(a,N);
/********** End **********/
/*(2)call the print()function*/
/**********Program**********/ Print(a);
/********** End **********/
}
void multiply(int a[],int n){
int I;
/*(3)double the value of the array elements*/
/**********Program**********/
/********** End ***********/
} void print(int *p){
printf(“nThe output values after multiply are:n”);
int i;
/*(4)output the value of the array elements*/
/**********Program**********/
/********** End **********/ }