简体   繁体   中英

How to reference a dynamically allocated character array in c++

I need to understand how to manipulate data in various index positions in a dynamically allocated character array in c++. I am making a quack (queue/stack). The variable I am concerned with is 'items.'

Here is the class definition for 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);
};

Now, here's the Quack constructor:

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

I understand that each instance of Quack has a char variable 'items' which is a pointer to a new character array. What I don't understand is how to reference the items in the array. For example, if I want to refer to index position 0 of the new character array, do I say items(0) , or items[0] , or is it something completely different?

Thank you

As you understood correctly, items is a char array and each instance of Quack has its own variable items . Accessing a dynamically allocated array and a statically allocated array is the same. They are allocated in contiguous memory locations.

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  

You can access the variable in any of the following ways:

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  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM