Câu hỏi: PRF192 - Phần 5: Con trỏ
41 câu trắc nghiệm kèm lời giải của môn PRF192 - Cơ sở lập trình, xếp theo thứ tự nhập vào ngân hàng. Trang 2/3 — câu 21–40.
Danh sách câu hỏi
#include<stdio.h>
void increment(int *ptr) {
(*ptr)++;
}
int main(){
int x=1005;
increment(&x);
printf("%d ",x);
}int a[5] = {1, 2, 3, 4, 5};
printf("%d", a);#include <stdio.h>
void swap(int a, int *b) {
int temp = a;
a = *b;
*b = temp;
}
int main() {
int x = 5;
int y = 10;
swap(x, y);
printf("x = %d, y = %d\n", x, y);
return 0;
}PointerAddressIntegerByteint main() {
int *p = NULL;
printf("%d",p);
getchar();
return 0;
}The output will be:
int main()
{
int i = 100;
int *p = &i;
*p +=2;
printf("%d, %d", i, *p);
getchar();
return 0;
}int a = 5, b = 7;
int *pa = &a, *pb = &b;
*pa = *pa + *pb;int arr[] = {1, 2, 3, 4};
printf("%d", *(arr + 2));#include <stdio.h>
void swap(int *px, int *py)
{
int z;
z=*px;
*px=*py ;
*py=z ;
}
int main()
{
int a=15, b=21 ;
swap(&a,&b);
printf ("%d %d",a,b);
return 0;
}pointersvaluetypeaddressvoid main()
{
int const *p = 5;
printf("%d", ++(*p));
getchar();
}#include <stdio.h>
int main()
{
int a = 4;
int *b = &a;
a -= 1;
*b -= 1;
printf("%d %d", a, *b);
return 0;
}void input(int* a, int n);Đáp án đúng: "All the above". Phần tử thứ tư nằm ở chỉ số 3 vì chỉ số đếm từ 0, và chuẩn C định nghĩa a[3] chính là *(a + 3), nên hai lối viết khác nhau trên mặt chữ lại sinh ra cùng một phép truy cập; lấy &a[3] thì chạm tới đúng phần tử đó ở mức địa chỉ. Cả ba lệnh in đều hợp lệ và đều nói về phần tử thứ tư, nên chỉ lựa chọn gộp mới trả lời đủ. Lệnh "printf("a[3] = %d\n", a[3]);" đúng nhưng chỉ là một trong ba cách. Lệnh "printf("*(a + 3) = %d\n", *(a + 3));" cũng đúng và theo định nghĩa là cùng một thao tác với lối dùng chỉ số, nên không thể đúng một mình. Lệnh "printf("&a[3] = %p\n", (void*)&a[3]); " in địa chỉ của phần tử thứ tư, vẫn là truy cập hợp lệ tới nó, chỉ khác là lấy địa chỉ thay vì lấy giá trị.
#include<stdio.h>
void updatePosition(int *time, float* position, float v)
{
if ((*position < 5) && (*position > -5))
*position += v;
else
*position += -0.1*(*position);
time += 1;
}
main()
{
float p = 4.0, v = 1;
int t = 0;
printf("t = %d, p = %f\n", t, p);
updatePosition(&t, &p, v);
updatePosition(&t, &p, v);
printf("t = %d, p = %f\n", t, p);
}What is the result displayed to the screen?
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
int main() {
int x = 5, y = 10;
swap(&x, &y);
printf("%d %d", x, y);
return 0;
}