Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am reading a binary file with fstream and storing the information in an array of characters:

int dataLength = 32;
int counter = 0;
char data[dataLength];
char PMTone[dataLength/4];
std::fstream *fs = new std::fstream(inputFileName,std::ios::in|std::ios::binary);
fs->read((char *)&data, dataLength);
//of the 32 characters in data[], I need first, 5th etc elements:
//fill first pmt info
for(int i=0; i<(dataLength/4); i++){
PMTone[i]=data[counter];
counter+=4;
}

Now I'm setting PMTone[7] to be a as a test:

PMTone[7] = "a";

I get the error:

mdfTree.cpp:92:21: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]

I don't understand why the elements in PMTone[] are pointers to chars, when PMTone[] was defined as an array of chars.

When I do treat PMTone[] as an array of pointers to chars:

(*PMTone)[7] = "a";

I get another error that I don't understand:

mdfTree.cpp:91:18: error: invalid types ‘char[int]’ for array subscript

This seems to imply that the compiler doesn't consider PMTone[] to be an array at all, but simply a pointer to char.

Can anyone shed light on what is happening here? Why has PMTone[] become an array of pointers to chars?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
173 views
Welcome To Ask or Share your Answers For Others

1 Answer

The literal "a" is not a character.

You need:

PMTone[7] = 'a';

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...