Hi all!!!
I'm starting to learn C++ on my own and I have a question about accessing a structure through a dynamic array. I've looked into some previous threads that apply to this but they all deal with structure composed of int variables. This concerns char and is driving me nuts.

#include <iostream>
using namespace std;

struct structname
{
   char pst[10];
};

int main(void)
{
   structname *pointername = new structname[3];
   pointername[0].pst = "Mocha";
   cout << pointername[0].pst << endl;
   delete pointername;   
   return 0 ;
}

I've attempted these syntaxes but to no avail
structname->membername=
(*structname).membername=
(structname).membername=

all I get is error message about const char* not being able to be converted to char.
Thanks for any help.

Recommended Answers

All 3 Replies

Member Avatar for jencas

Use std::string pst instead of char pst[]. Or you have to use strcpy() to assign a value.

thanks so much
I'll give it a shot and see how it goes.

Yeah, STL is the better way to go for C++.

Also, use delete[] pointername; instead of just delete.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.