0

I'm a little confused about the teacher solutions to this problem set below.

I thought when you declare a pointer variable and then set to an array you do something like:

char *myPointer;

And then maybe in the constructor you do something like

myPointer = new char[20];

why declare char *symbol[20] as a class member variable? What does that mean exactly? If it's a static array, why not just declare char symbol[20]; ?

Question and Solution

Assume a stock class has symbol (char *), cost (int) and shares (int) as private members. Write the minimum stock class declarations that supports stock s2(“APPL”, 209, 7), s3(“FB”, 80); // 7 shares of APPL at $209, 5 share of FB at $80 << hint: really minimum… no other functions… >> << hint: make it easier… assume symbol can have at most 20 characters…>>

class stock {
public:
stock(char * s, int c, int sh=5);
private:
char * symbol[20];
int cost;
int shares;
}

Write the implementation functions for the above declaration


stock::stock(char *s, int c, int sh) {
    strcpy(symbol, s);  // or other means of deep copy, no point if no deep copy
    cost = c;
    shares = s;
}


`````````````````````
2
  • That should not compile. char* symbol[20] would be an array of twenty pointers. Not what you want in this scenario. Commented Sep 24, 2019 at 1:53
  • It's an array of char*, not an array of char. However the description sounds like that's not what the teacher actually wants. This is not a question to ask us, though. You should ask your teacher. Commented Sep 24, 2019 at 1:54

2 Answers 2

1

You are completely right, there is no need to make symbol an array of pointers to characters. You're passing in a string (like "APPL"), which is an array characters, so symbol should be an array of char, not an array of char*.

What char* symbol[20] can mean is an array of 20 strings, where each pointer in the array can perhaps point to the beginning of an array of char.

What you really should be using is std::string, but if your teacher hasn't covered that yet then just char symbol[20] is fine.

Sign up to request clarification or add additional context in comments.

Comments

1

What does [char *symbol[20]] mean exactly?

It means an array of 20 pointers to char.

why declare [that] as a class member variable?

In general: It can be declared in cases where a class needs to have 20 pointers that can be accessed by index. That's what arrays are for.

In case of the specific problem: It makes no sense to have an array of 20 pointers to refer to a single string.

If it's a static array,

It's not "static". It is a (non-static) member variable.

why not just declare char symbol[20];

In general: Because an array of characters is a completely different thing than an array of pointers. It is not immediately obvious how a problem that can be solved by 20 pointers could easily be solved with 20 characters.

In case of the specific problem: That makes more sense than an array of pointers. It does have a downside that the size is limited to 20 characters, so it is not useful in practice, but within the confines of the exercise, it would be sufficient. For practical use case, std::string would be better since it is not limited to an arbitrary length.

In short: There is presumably a bug in the given class.

2 Comments

Judging from the quote "Write the minimum stock class declarations that supports stock s2(“APPL”, 209, 7), s3(“FB”, 80)" I think they just want to hold a single string in the stock class, not twenty strings.
@0x499602D2 fair enough. Edited accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.