动态创建数组的sizeof字符数组 [英] sizeof character array for a dynamically created array

查看:59
本文介绍了动态创建数组的sizeof字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据模式长度(即 plen )动态创建一个char数组.但是,当我执行 sizeof(table)时,得到8.为什么我得到的是 8 而不是 3 ?

I need to create a char array dynamically based on the pattern length, i.e., plen. However, when I do sizeof(table), I get 8. Why am I getting 8 instead of 3?

int main() {
    char *pattern = "aaa";
    int plen = strlen(pattern);
    char *table = new char[plen + 1];
    for(int i = 0; i < plen; i++) {
        table[i] = pattern[i];
    }
    cout << sizeof(table) << plen << table;
    return 0;
}

我得到的输出是 83aaa ... : sizeof(table) 8 ,对于 sizeof(table) 3 table plen aaa ,具有模式的存储值.

The output I get is 83aaa...: 8 for sizeof(table), 3 for plen and aaa for table, which has the stored value of pattern.

推荐答案

这是因为 table 是指针,而不是数组.在您的体系结构中,指针的大小为8个字节.

It is because table is a pointer, not an array. And the size of a pointer in your architecture is 8 bytes.

这篇关于动态创建数组的sizeof字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆