c++ - 链表数组大小不同

标签 c++ arrays struct linked-list

<分区>

这是我的代码:

using namespace std;
class Network
{
    // private function to constructe the nodes with a left and right childed
    private:

        struct node
        {


           node* left;
           node* mid;
           node* right;
           char data;
        };
        node* root;
    // public function for the the program
    public:
        char *Nodes ;
        Network()
        {
           root = NULL;
        }

        char Link(char,char);



        node* Network::Order(char* temp){


            cout << "Order:" << sizeof(temp);

            node* store; 
            store = new node[sizeof(temp)];



};
};


char Network::Link(char temp, char temp1){

};




// Smaller elements go left
// larger elements go right



/*
 * 
 */
int main() {

    Network object;
    char p[11];
    p[0] = 'a';
    p[1] = 'b';

    cout << "p:" << sizeof(p);
    object.Order(p);
    return 0;
}

输出:p:11 订单:4

为什么顺序 4 应该是 11 因为我传递了数组 p t

最佳答案

当传递给需要指针的函数时,您的数组会衰减为指针。 Order 函数中的 sizeof 运算符返回指针的大小,在您的体系结构中为 4 个字节。

(C99, 6.3.2.1p3) "Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type "array of type" is converted to an expression with type "pointer to type" that points to the initial element of the array object and is not an lvalue."

关于c++ - 链表数组大小不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11623568/

相关文章:

c++ - 为什么短裤不应该使用整数文字?

c++ - 在 C++ 中拆分 `char[]`

c++ - 学习 C++ : error: use of deleted function

linux - 通过recv C/C++ 通过套接字传递结构

c - 打印字符数组的前几个字符?

似乎可以在结构内部传递结构,有什么想法吗?

c++ - 使用 OpenCV 将帧转换为就像从上方获取的一样

c++ - 为什么要定义一个返回结构的 lambda 函数而不是直接定义结构?

[Array of Struct in [Array of Struct in [Array of Struct ]]] 中的结构体动态数组

java - 在java中处理windows-1252和unicode