C++ : Cout cái gì?

Chương trình sau đây sẽ không in ra cái ta muốn xem. Đố: Tại sao? Và sửa lại như thế nào?

#include <iostream>
using namespace std;

int main() {
    int a = 123;
    char b = '4';
    cout << "Address of a = " << &a << endl;
    cout << "Address of b = " << &b << endl;
    return 0;
}

Chủ đề : Lập trình, Vui - Giải Trí and tagged , , . Bookmark the permalink. Trackbacks are closed, but you can post a comment.

4 Comments

  1. Hùng
    Posted 26/01/2012 at 12:17 pm | Permalink

    Địa chỉ của một biến kiểu char sẽ được xem như là một string trong C/C++.

    Muốn in địa chỉ b ra thì cần ép về một kiểu con trỏ nào đấy (int *) hoặc (long *).

    • Posted 26/01/2012 at 12:21 pm | Permalink

      Chính xác. Bạn này nhanh quá. Lần sau phải đố khó hơn xíu :-)

    • VietWOW
      Posted 30/01/2012 at 12:32 am | Permalink

      Đơn giản là cast về dạng (void *) cũng được :D

  2. Cuong
    Posted 29/01/2012 at 11:51 pm | Permalink

    Có người trả lời mất rồi :( . Nghịch chút cho vui:

    int a = 1414350923;

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>