Con trỏ, mảng, string

1. What is output ?

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

void myfunc(char** param)
{
    ++param;
}
void main()
{
    char* string = (char*)malloc(64);
    strcpy(string, "hello_World");
    myfunc(&string);
    myfunc(&string);
    printf("%s\n", string);
    getch();
}

A. hello_World
B. ello_World
C. llo_World
D. lo_World

Đáp án
A
Giải thích: biến string sẽ chứa địa chỉ của chuỗi. &string là địa chỉ của biến con trỏ string (xem hình vẽ). Khi truyền &string cho hàm myfunc(), thì param = &string (tức là con trỏ 2 chiều param trỏ tới địa chỉ của biến string). Trong hàm myfunc(), phép toán ++param làm thay đổi vùng nhớ mà param trỏ tới mà không làm thay đổi địa chỉ mà biến string trỏ tới.

2. What is output?

void myfunc(char** param)
{
    ++*param;
}
void main()
{
    char* string = (char*)malloc(64);
    strcpy(string, "hello_World");
    myfunc(&string);
    myfunc(&string);
    printf("%s\n", string);
    getch();
}

A. hello_World
B. ello_World
C. llo_World
D. lo_World

Đáp án

C
Giải thích: biến string sẽ chứa địa chỉ của chuỗi. &string là địa chỉ của biến con trỏ string (xem hình vẽ). Khi truyền &string cho hàm myfunc(), thì param = &string (tức là con trỏ 2 chiều param trỏ tới địa chỉ của biến string). Trong hàm myfunc(), phép toán ++*param = ++string làm thay đổi vùng nhớ mà string trỏ tới.

Con trỏ và chuỗi
Con trỏ và chuỗi

 

3. What is output?

void main()
{
    int ints[] = { 0, 1, 2, 3 };
    int* i1 = ints + 1;
    int a = ++*i1;
    int b = a + *i1;
    printf("%d\n", b);
    getch();
}

A. 4
B. 3
C. 6

Đáp án

A
Giải thích: i1 = ints+1, i1 sẽ trỏ tới phần tử thứ 2 của mảng ints[] và *i1 = 1. Lệnh ++*i1 = ++(*i)1, lệnh này sẽ thay đổi giá trị tại vùng nhớ mà i1 trỏ tới (sau khi lệnh này được thực hiện,*i1 = 2 hay nói cách khác ints[] = {0,2,2,3}. Xem hình vẽ:

Con trỏ và mảng
Con trỏ và mảng

 

4. What is output?

void main()
{
    int ints[] = { 0, 5, 10, 15 };
    int* i2 = ints + 2;
    int a = *i2++; // a = *(i2++);
    printf("%d#%d\n", a, *i2);
    getch();
}

A. 10#15
B. 10#10
C. 15#15
D. 11#15

Đáp án

A
Giải thích: i2++ được thực hiện nhưng giá trị của i2 sẽ được thay đổi sau khi sử dụng. Sau khi thực hiện a = *(i2++), a = *i2 = 10, i2 = i2 + 1.

 

5. What is output of following code?

void main()
{
    int ints[] = { 0, 1, 2, 3 };
    int* i1 = ints + 1;
    int* i2 = ints + 2;
    int a = ++*i1 + *i2++;
    int b = *++i1 + *i2--;
    printf("%d#%d", a, b);
    getch();
}

A. 4#4
B. 4#5
C. 5#6
D. 4#6

Đáp án

B
Giải thích:
– Lệnh int a = ++*i1 + *i2++;, toán tử * và ++ cùng thứ tự ưu tiên nhưng có thứ tự kết hợp từ phải sang trái. i2++ sẽ được thực hiện trước nhưng i2 sẽ trỏ tới phần tử tiếp theo sau khi i2 được sử dụng( nghĩa là *(i2++) = 2). ++*i1 = ++(i*) = ints[1] = 2. a = 2 + 2 = 4. Sau lệnh này, i2 trỏ tới phần tử cuối cùng của mảng ints[],i1 trỏ tới phần tử thứ 2 của mảng ints[], giá trị ints[1] bị thay đổi (=2).
– Lệnh int b = *++i1 + *i2–;, toán tử * và — cùng thứ tự ưu tiên nhưng có thứ tự kết hợp từ phải sang trái. i2– được thực hiện trước nhưng i2 sẽ trỏ tới phần tử thứ 3 của mảng sau khi i2 được sử dụng( nghĩa là *(i2–) = 3). *++i1 = *(++i1) = ints[2] = 2. b = 2 + 3 = 5.

 

6. What is output of following code?

void main()
{
    int i = 400;
    int *ptr = &i;
    *++ptr = 2;
    printf("%d %d", i, *ptr);
    getch();
}

A. 400 2
B. 400 400
C. 400 401
D. Complier error

Đáp án

A
Giải thích: Lệnh *++ptr = 2<=> *(++ptr) = 2, ptr sẽ trỏ đến vùng nhớ khác và gán giá trị cho vùng nhớ đó = 2.

 

7. What is output?

void main()
{
    char str[] = {"pvpit"};
    char *s1 = str;
    s1++;
    printf("%c", *s1);
    getch();
}

 

A. pvpit
B. vpit
C. v
D. Another

Đáp án

C
Giải thích: s1 = str, s1 trỏ tới phần tử đầu tiên của chuỗi “pvpit”. s1++, s1 trỏ tới phần tử thứ 2 của chuỗi “pvpit”. *s1 = ‘v’.

 

8. What is output?

void main()
{
    char *s = "\12345s\n";
    printf("%d", strlen(s));
    printf("\n%s", s);
    getch();
}

 

A. 5
B. 7
C. 9
D. 10
Đáp án

A

 

9. For the code below which lines should be reported as errors by a compiler?

int main(int argc, char** argv)
{
    const char* foo = "wow"; // line 1
    foo = "top"; // line 2
    foo[0] = 1; // line 3
    return 0;
}

A. 1
B. 2
C. 3
D. None of the lines

Đáp án

C
Giải thích: biến foo là biến const, trình biên dịch không cho phép thay đổi giá trị của biến foo.

 

10. What is output?

void main() 
{ 
    int x = 5,y = 6;
    int* const p = &x;
    p = &y; 
    printf("%d", (*p)); 
    getch();
}

A. Complier error
B. 6
C. 5
D. Another

Đáp án

A
Giải thích: p là hằng con trỏ (constant pointer). Khi khai báo hằng con trỏ, ta cần khởi tạo luôn cho hằng con trỏ (Nếu không khởi tạo, chương trình biên dịch (complier) gây ra lỗi). Trình biên dịch sẽ không cho phép thay đổi vùng nhớ mà con trỏ p trỏ tới. p = &y → gây ra lỗi.

 

11. What is output?

void main() 
{ 
    int x = 5, y = 8;
    const int* p;
    p = &x;
    p = &y;
    x++;
    printf("%d", *p); 
    getch();
}

A. 5
B. 6
C. 8
D. Complier Error

Đáp án

C
Giải thích: p là con trỏ tới hằng số (pointer to constant), nói cách khác ta không thể dùng p để thay đổi giá trị của vùng nhớ mà p trỏ đến. Chú ý: p có thể trỏ tới vùng nhớ khác.

 

12. What is output of code?

void main() 
{ 
    int x = 5;
    const int* p;
    p = &x;
    x++;
    *p = 4;
    printf("%d", *p); 
    getch();
}

A. 5
B. 6
C. 4
D. Complier Error

Đáp án

D
Giải thích: p là con trỏ tới hằng số (pointer to constant), nói cách khác ta không thể dùng p để thay đổi giá trị của vùng nhớ mà p trỏ đến. Nhưng ta có thể thay đổi giá trị của vùng nhớ đó bằng chính biến đó (x++ là OK). *p = 4 –> gây lỗi.

 

13. What is output of code?

#include <stdio.h>

int main()
{
    int a = 320;
    char *ptr;
    ptr = (char*)&a;
    printf("%d ", *ptr);
    return 0;
}

A. 320
B. 64
C. Complier Error

Đáp án

B

 

14. What will be output of following program?

#include <stdio.h>

int main()
{
    int i = 3;
    int *j;
    int **k;
    j = &i;
    k = &j;
    printf("%u , %u , %d ", k, *k, **k);
    return 0;
}

A. Address of j , Address of  i , 3
B. Complier Error
C. 3 , 3 , 3

Đáp án
A
Giải thích: k = &j <=> *k = *&j <=> *k = j <=> **k = *j.

 

15. What will be output of following program?

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

int main()
{
    char *ptr1 = NULL;
    char *ptr2 = 0;
    printf("\n%d", ptr2);
    strcpy(ptr1, "c");
    strcpy(ptr2, "questions");
    printf("\n%s %s", ptr1, ptr2);
    getch();
}
Đáp án
Chương trình biên dịch (Complier) báo lỗi tại dòng strcpy(ptr1," c");. Vì biến ptr1 chưa được cấp phát.
Xem thêm các lỗi hay gặp khi lập trình C

 

16.  What will be output of following program?

#include <stdio.h>
#include <conio.h>

int main()
{
    int a = 10;
    void *p = &a;
    int *ptr = p;
    printf("%u\n", *ptr);
    getch();
}
Đáp án
Lỗi tại dòng int *ptr = p;. Xem bài viết con trỏ void

 

17.What will be output of following program?

#include <stdio.h>
#include <conio.h>

int main()
{
    int a = 5, b = 10, c;
    int *p = &a, *q = &b;
    c = p - q;
    printf("%d" , c);
    getch();
}

A. 3
B. 4
C. 6

Đáp án

A

 

18. What will be output of following program?

#include <stdio.h>
#include <conio.h>

int main()
{
    int i = 5, j;
    int *p , *q;
    p = &i;
    q = &j;
    j = 5;
    printf("%d %d", *p, *q);
    getch();
}

A. 5 5
B. Complier Error
C. 5 Garbage value

Đáp án
A

 

19. What will be output of following program?

#include <stdio.h>
#include <conio.h>

void main()
{
    int i = 5;
    int *p;
    p = &i;
    printf(" %u %u", *&p , &*p);
    getch();
}

A. Address of i Address of i
B. Garbage value Garbage value
C. Complier Error

Đáp án
A
Giải thích: *&p = &*p = p = &i

 

20. What is output?

#include <stdio.h>
#include <conio.h>
int main()
{
    int array[2][2][3]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    printf("%d", array[1][0][2]);
    getch();
}

A. 6
B. 7
C. 8
D. 9

Đáp án

C

 

21. What is output?

#include <stdio.h>
#include <conio.h>
void main()
{
    char arr[8]={'V','I','E','T','N','A','M'}; 
    char *p; 
    p=(char *)(arr+2)[2]; 
    printf("%c", p);  
    getch();
}

A. I
B. E
C. M
D. N

Đáp án

D

 

22. What will be output of following program?

#include <stdio.h>
#include <conio.h>
void main()
{
    char ch[]={'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    int *p = (int*)ch;
    p++;
    printf("%x", *p);
    getch();
}

(Giả sử: kiến trúc máy tính sử dụng là little endian)
A. 37363534
B. 34353637
C. 45673333

Đáp án

A

 

23. What is output?

#include <stdio.h>
#include <conio.h>

int main()
{
    int i = 11;
    int const * p = &i;
    p++;
    printf("%d", *p); 
    getch();
}

A. 11
B. 12
C. Garbage value
D. Complier error

Đáp án

C

 

24. Which of the following statements are correct about an array?
1. The array int num[26]; can store 26 elements
2. The expression num[1] designates the very first element in the array
3. It is necessary to initialize the array at the time of declaration.
4. The declaration num[SIZE] is allowed if SIZE is a macro.

A. 1,4
B. 3
C. 1,2
D. 1

Đáp án

A

 

25. The library function used to find the last occurrence of a character in a string is
A. strnstr()
B. strrchr()
C. laststr()
D. strstr()

Đáp án

B

 

26. What is output? (assuming that the array begins at the location 1002 and size of an integer is 4 bytes)

#include <stdio.h>
#include  <conio.h>
int main()
{
    int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
    printf("%u, %u, %u\n", a[0]+1, *(a[0]+1), *(*(a+0)+1));
    getch();
}

A. 1006, 2, 2
B. 1006, 4, 4
C. 1002, 5, 5
D. Error

Đáp án

A

 

27. What does the following declaration mean?

int (*ptr)[10];

A. ptr is array of pointers to 10 integers
B. ptr is a pointer to an array of 10 integers
C. ptr is an array of 10 integers
D. ptr is an pointer to array

Đáp án

A

 

28. What is output?

#include <stdio.h>
#include <conio.h>
int main()
{
    char str[] = "VNCODING\0\.NET\0";
    printf("%s\n", str);
    getch();
}

A. VNCODING
B. VNCODING\0\.NET\0
C. VNCODING\0\.NET

Đáp án

A

 

29. What is output?

#include <stdio.h>
#include <conio.h>
void swap(char *, char *);

int main()
{
    char *pstr[2] = {"VNCODING", ".NET"};
    swap(pstr[0], pstr[1]);
    printf("%s%s", pstr[0], pstr[1]);
    getch();
}
void swap(char *t1, char *t2)
{
    char *t;
    t=t1;
    t1=t2;
    t2=t;
}

A. VNCODING.NET
B. .NETVNCODING
C. Address of pstr[0] Address of pstr[1] Đáp án

A

 

30. What is output?

#include <stdio.h>
#include <conio.h>

void swap(char **, char **);

int main()
{
    char *pstr[2] = {"VNCODING", ".NET"};
    swap(&pstr[0], &pstr[1]);
    printf("%s%s", pstr[0], pstr[1]);
    getch();
}
void swap(char **t1, char **t2)
{
    char *t;
    t=*t1;
    *t1=*t2;
    *t2=t;
}

A. VNCODING.NET
B. .NETVNCODING
C. Address of pstr[0] Address of pstr[1] Đáp án

B

5 Comments on Con trỏ, mảng, string

  1. A ơi anh giải thích giúp em kết quả đoạn code này được không ạ? Em cảm ơn!
    what is the output when the sample code below is executed?
    #include
    void main(){
    int i,j=25;
    int *pi, **pj=&j;
    ***pj=j+5;
    i=*pj+5;
    pi=pj;
    **pi=i+j;
    printf(“%d%d”,**pi,pj);}

Leave a Reply