Answer the question
In order to leave comments, you need to log in
No output from array with pointers from c++ structure?
// Example program
#include <iostream>
#include <string>
using namespace std;
struct Sentence{
char** words;
int size;
};
struct Text{
Sentence *sentences;
int size;
};
char * add_element(char *massiv, int number){
char* tp = new char[number+1];
for (int i=0; i<number; i++) tp[i] = massiv[i];
delete[] massiv;
return tp;
}
int main()
{
Text text;
char words[] = {'A'};
char* sent[1];
sent[0] = words;
text.sentences->words = sent;
char* word = add_element(text.sentences->words[0], 1);
text.sentences->words[0] = word;
cout << text.sentences->words[0][0];
}
Answer the question
In order to leave comments, you need to log in
First and foremost. It's amazing that the program worked for you, because text.sentences is not initialized. I have a flight.
Well, the eternal mistake of a beginner “plus”: memory is allocated, it is not clear who owns what, and, of course, the memory “flows”. Why, excuse me, did C++ make encapsulation, constructors and destructors?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question