如何在C ++中引用动态分配的字符数组

巴巴·斯沃洛奇

我需要了解如何在c ++中动态分配的字符数组中的各个索引位置中操纵数据。我正在发出嘎嘎声(队列/堆栈)。我关注的变量是“项目”。

这是Quack的类定义:

class Quack
{
public:
    static const char   YOUR_NAME[];        // used for printing out programmer's name
    static const bool   PREMIUM_VERSION;    // used to designate Regular vs. Premium

    Quack(int capacity, int growBy = 0);
        // capacity: # of slots in array
        // growBy:   # of slots to add to array when it grows, 0 means "don't grow"
    ~Quack(void);
    bool pushFront(const char ch);      // push an item onto the front
    bool pushBack(const char ch);       // push an item onto the back
    bool popFront(char& ch);            // pop an item off the front
    bool popBack(char& ch);             // pop an item off the back
    void rotate(int r);                 // "rotate" the stored items (see note below)
    void reverse(void);                 // reverse the order of the stored items
    int itemCount(void);                // return the current number of stored items

    void printArray(std::ostream& out)                  // print contents of array
    {
        unsigned int    ch;

        out << "[ ";
        for (int i = 0; i < capacity; i++) {
            ch = static_cast<unsigned char>(items[i]);  // avoid sign extension
            if (ch == '-')                              // print in hex, because using '-' for 0xCD
                goto printHex;
            else if (ch >= '!' && ch <= '}')            // ASCII text character
                out << static_cast<char>(ch) << ' ';
            else if (ch == 0xCD)                        // print 0xCD as '-'
                out << "- ";
            else                                        // print everything else as hex
                goto printHex;
            continue;

        printHex:   
            out << std::setw(2) << std::setfill('0') << std::hex << ch << ' ';
            }
        out << ']' << std::endl;
    }

private:
    char    *items;                     
    int     nItems;                     
    int     capacity;                   
    int     growBy;
    int     *front;
    int     *back;

public:
    friend std::ostream& operator<<(std::ostream& out, Quack *q);
};

现在,这是Quack构造函数:

Quack::Quack(int capacity, int growBy) :
    capacity(capacity),
    growBy(growBy),
    nItems(0),
    items(new char[capacity]),
    front(NULL),
    back(NULL)
{
}

我知道Quack的每个实例都有一个char变量“ items”,它是指向新字符数组的指针。我不明白的是如何引用数组中的项目。例如,如果我要引用新字符数组的索引位置0,我说的是items(0)还是items [0],还是完全不同?

谢谢

cppcoder

如您所正确理解的那样,它items是一个char数组,每个的实例Quack都有自己的变量items访问动态分配的数组和静态分配的数组是相同的。它们被分配在连续的内存位置中。

char arr[24]; // Allocate 24 bytes in contiguous memory locations in stack  
char* arr = new char[24]; //Allocate 24 bytes in contiguous memory locations
                          // in heap  

您可以通过以下任意一种方式访问​​变量:

1.  Quack q;  
    cout << q.items << endl; //Print the char array  
    cout << q.items[1] << endl; // Print the second index position of items  

2.  Quack* qptr = new Quack();
    cout << q->items << endl; //Print the char array  
    cout << q->items[1] << endl; // Print the second index position of items  

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在C中动态分配的字符串数组

来自分类Dev

在C中动态分配的字符串数组

来自分类Dev

如何在 C++ 中的全局字符数组中动态分配类的实例?

来自分类Dev

如何在C中为字符串数组动态分配内存?

来自分类Dev

C ++中的动态分配数组

来自分类Dev

C ++中的动态分配数组

来自分类Dev

如何在C ++中删除由动态分配的数组成员组成的动态分配的结构?

来自分类Dev

尝试在C中创建动态分配的字符串数组时出现分段错误

来自分类Dev

使用C中的动态分配创建2D字符串数组

来自分类Dev

c中的字符矩阵(动态分配的二维数组)

来自分类Dev

如何动态分配二维字符数组?

来自分类Dev

在C ++中为联合中的数组动态分配内存

来自分类Dev

在C中的函数中动态分配数组

来自分类Dev

如何在C ++中对静态动态分配的数组进行静态初始化?

来自分类Dev

如何在C ++中为char数组动态分配内存?

来自分类Dev

如何在for循环中释放C中动态分配的数组

来自分类Dev

如何在C ++中动态分配2D数组?

来自分类Dev

如何在C ++中将逗号分隔的数字矩阵放入动态分配的数组中?

来自分类Dev

如何在C中创建动态分配的字符串?

来自分类Dev

动态分配的字符串文字的间接操作如何在 C 中真正起作用?

来自分类Dev

在C中动态分配2D数组

来自分类Dev

在C中动态分配2D数组

来自分类Dev

在结构中动态分配数组-C

来自分类Dev

在 C++ 中销毁动态分配的内存(数组对象)

来自分类Dev

C中的字符串动态分配

来自分类Dev

C中的字符串动态分配

来自分类Dev

如何在C ++中访问动态分配的矩阵?

来自分类Dev

如何在C中动态分配静态存储?

来自分类Dev

如何扩展动态分配的数组

Related 相关文章

热门标签

归档