簡體   English   中英

如何在C ++中引用動態分配的字符數組

[英]How to reference a dynamically allocated character array in 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],還是完全不同?

謝謝

如您所正確理解的, 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  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM