Problems comparing strings (arduino) - c++

I have problems with comparing strings.
I receive from mqtt a number of messages, I want to store the latest value for each topic (key)
I have shown the pertinent parts of my sketch.
#define MAX_SENSORS 6
// table of topic and value
char sensor_data[MAX_SENSORS][2][20];
initialize table
void init() {
....
for (int i=0;i<MAX_SENSORS;i++) // initialize table
{
strcpy(sensor_data[i][0],"?");
strcpy(sensor_data[i][1],"");
}
}
Message received here
void callback(char* topic, byte* b_payload, unsigned int length) {
// Convert byte* b_payload to string
for (int i = 0; i < length; i++) {
payload[i]=(char)b_payload[i];
};
payload[length]='\0';
for (int i = 0; i < MAX_SENSORS; i++)
{
Why does this (desparate debug) not work?
if (sensor_data[i][0]=="?") {
Serial.print("*");
}
Original
if (sensor_data[i][0]==topic) { // Slot matching topic found
strcpy(sensor_data[i][1],payload);
Serial.printf(" Slot %d",i);
break;
} else if (sensor_data[i][0]=="?") { // Empty slot found, store topic here
strcpy(sensor_data[i][0],topic);
strcpy(sensor_data[i][1],payload);
Serial.printf(" New %d",i);
break;
}
}
Serial.println();
}
It all probably originates because I'm confused between strings and char arrays?
edit
The assignment
strcpy(sensor_data[i][0],topic);
strcpy(sensor_data[i][1],payload);
seem to assign
concat(topic,payload) to sensor_data[i][0].

It all probably originates because I'm confused between strings and
char arrays?
Seems like it. C-strings (char* and char[]) are not comparable with ==, arduino's String class is. Either convert the left side of the comparison to String or use strcmp to compare the strings.
if (String(sensor_data[i][0])=="?") {
if (strcmp(sensor_data[i][0], "?") == 0) {

Related

Sending an array of pointers as an argument to another function

I'm writing a game script, sort of in vein of 80's Rouge. My problem is that I want to send a pointer, *p_pixel_grid[8192], to another function without changing it. The other function has to recall the grid via this pointer, it supposed to have access to that part of the memory. It is necessary to redraw the grid at a later stage. Im unable to find a way, or syntax, to send it further. Any suggestions?
char grid_pointer(char grid[8192]) *//this function is just a test function to check if I can send it a pointer as an argument*
{
for (int i=0; i<=8191; i++)
{
cout << grid[8192];
cout << "a";
}
}
void lvl_loader ()
{
FILE* plik;
switch(menu())
{
case 1:
{
plik = fopen("lvl 1.txt", "r");
break;
}
case 2:
{
plik = fopen("lvl 2.txt", "r");
break;
}
case 3:
{
plik = fopen("lvl 3.txt", "r");
break;
}
}
char pixel_grid[32][128];
for (int i=0; i<=31; i++)
{
for (int j=0; j<=127; j++)
{
pixel_grid[i][j] = fgetc(plik);
//cout << pixel_grid[i][j];
}
}fclose(plik);
char *p_pixel_grid[8192]; int pointer_counter=1;
for (int i=0; i<=31; i++)
{
for (int j=0; j<=127; j++)
{
p_pixel_grid[pointer_counter]=&pixel_grid[i][j];
cout << *p_pixel_grid[pointer_counter];
pointer_counter++;
}
}
grid_pointer(&*p_pixel_grid[8192]); //here, I want send this pointer to another function, possibly unchanged. The other function has to use that pointer to recall to data loaded from the .txt
}
void grid_pointer(char ptr[][128])
{
std::cout << ptr[0][1];
std::cout << ptr[10][10];
}
int main()
{
char pixel_grid[32][128];
pixel_grid[0][1] = 'a';
pixel_grid[10][10] = 'b';
grid_pointer(pixel_grid);
}
I'm not sure how true this declaration
is here char *p_pixel_grid[8192]; because arrays are implicitly converted to pointers to their first element.
That is, it is better to make char p_pixel_grid[8192];
Passing a pointer to the array char grid_pointer(char grid[8192]) to the function; Also performed a little incorrectly. The required syntax looks like this:
void grid_pointer(char ptr[][128])
And instead of passing two parameters to the function - the pointer and the size. It is advisable to use span, it describes an object that can refer to a continuous sequence of objects with the first element of the sequence in the zero position.
#include <span>
#include <iostream>
void grid_pointer(std::span<char> grid)
{
std::cout << grid.data() << std::endl;
}
int main()
{
char p_pixel_grid[8192];
p_pixel_grid[0] = 'a';
p_pixel_grid[1] = 'b';
grid_pointer(std::span{ p_pixel_grid });
}
DEMO

Pass Pointer to function or array to function

I am trying to figure out what I am doing wrong. I have the problem down to a few lines of code. The program will freeze or behave incorrectly when code is used.
Just wondering if I am passing this correctly for the array's?
void callFunction(uint8_t &channel)
{
uint8_t address[8]; //1wire wire address and CRC
uint8_t scratchPad[8]; //holding for onewire capture
readScratchPad(address,scratchPad,channel);
}
void Temperature::readScratchPad(uint8_t rom[8],uint8_t data[8],const uint8_t &channel)
{
//read temperature data.
wireReset(); //reset();
selectChannel(channel); //necessary on -800
wireSelect(rom);
wireWriteByte(0xbe); // Read Scratchpad command
//display hex values of scratchpad
for ( int i = 0; i < 8; i++) { // we need 9 bytes
data[i] = wireReadByte();
}
}
The only other thing it could be is that I am passing a pointer to a function incorrectly like this:
function getNext()
{
uint8_t address[8];
getNextAddress(address);
}
bool Temperature::getNextAddress(uint8_t *vAddress)
{
if ( !wireSearch(vAddress))
{
wireResetSearch();
wireReset();
wireSkip();
return false;
}
return true;
}
I was passing the pointer as getNext(&address[8]), but it did not work either.
Thanks,

Returning a string * type array from a function back into the main

I'm new to C++ and I am working on a function to shuffle strings
It takes an array of strings, shuffles them, and returns them back to the main.
I am returning a pointer to an array of strings called shuffled. The problem I have is that when I try to save that new pointer to the array to another pointer in the main, I start getting weird values that either reference to a file location in my computer or a bunch of numbers.
I'll post the entire code here but really what you want to look at is the return types, how I return it and how I save it in main. Please tell me why my pointer is not referencing the working array that is created in the function. Here's the code:
#include <cstdio>
#include <string>
#include <ctime>
#include <new>
#include <cstdlib>
using namespace std;
const char * getString(const char * theStrings[], unsigned int stringNum)
{
return theStrings[stringNum];
}
string * shuffleStrings(string theStrings[])
{
int sz = 0;
while(!theStrings[sz].empty())
{
sz++;
}
sz--;
int randList[sz];
for(int p = 0; p < sz; p++)
{
randList[p] = sz;
}
srand(time(0));//seed randomizer to current time in seconds
bool ordered = true;
while(ordered)
{
int countNumberInRandList = 0;//avoid having a sz-1 member list length (weird error I was getting)
for(int i = 0; i < sz; i++)
{
int count = 0;
int randNum = rand()%(sz+1);//get random mod-based on size
for(int u = 0; u < sz; u++)
{
if(randList[u] != randNum)
{
count++;
}
}
if(count == sz)
{
randList[i] = randNum;
countNumberInRandList++;
}
else
i--;
}
//check to see if order is same
int count2 = 0;
for(int p = 0; p < sz; p++)
{
if(randList[p] == p)
{
count2++;
}
}
if(count2 < sz-(sz/2) && countNumberInRandList == sz)
{
ordered = false;
}
}
string * shuffled[sz];
for(int r = 0; r < sz; r++) //getting random num, and str list pointer from passed in stringlist and setting that value at shuffled [ random ].
{
int randVal = randList[r];
string * strListPointer = &theStrings[r];
shuffled[randVal] = strListPointer;
}
for(int i = 0; i < sz; i++)
{
printf("element %d is %s\n", i, shuffled[i]->c_str());//correct values in a random order.
}
return *shuffled;
}
int main()
{
string theSt[] = {"a", "b", "pocahontas","cashee","rawr", "okc", "mexican", "alfredo"};
string * shuff = shuffleStrings(theSt);//if looped, you will get wrong values
return 0;
}
Strings allocate their own memory, no need to give them the "length" like you would have to do for char arrays. There are several issues with your code - without going into the details, here are a few working/non-working examples that will hopefully help you:
using std::string;
// Returns a string by value
string s1() {
return "hello"; // This implicitly creates a std::string
}
// Also returns a string by value
string s2() {
string s = "how are you";
return s;
}
// Returns a pointer to a string - the caller is responsible for deleting
string* s3() {
string* s = new string;
*s = "this is a string";
return s;
}
// Does not work - do not use!
string* this_does_not_work() {
string s = "i am another string";
// Here we are returning a pointer to a locally allocated string.
// The string will be destroyed when this function returns, and the
// pointer will point at some random memory, not a string!
// Do not do this!
return &s;
}
int main() {
string v1 = s1();
// ...do things with v1...
string v2 = s2();
// ...do things with v2...
string* v3 = s3();
// ...do things with v3...
// We now own v3 and have to deallocate it!
delete v3;
}
There are a bunch of things wrong here -- don't panic, this is what happens to most people when they are first wrapping their brains around pointers and arrays in C and C++. But it means it's hard to put a finger on a single error and say "this is it". So I'll point out a few things.
(But advance warning: You ask about the pointer being returned to main, your code does indeed do something wrong with that, and I am about to say a bunch of things about what's wrong and how to do better. But that is not actually responsible for the errors you're seeing.)
So, in shuffleStrings you're making an array of pointers-to-string (string * shuffled[]). You're asking shuffleStrings to return a single pointer-to-string (string *). Can you see that these don't match?
In C and C++, you can't actually pass arrays around and return them from functions. The behaviour you get when you try tends to be confusing to newcomers. You'll need to understand it at some point, but for now I'll just say: you shouldn't actually be making shuffleStrings try to return an array.
There are two better approaches. The first is to use not an array but a vector, a container type that exists in C++ but not in C. You can pass arrays around by value, and they will get copied as required. If you made shuffleStrings return a vector<string*> (and made the other necessary changes in shuffleStrings and main to use vectors instead of arrays), that could work.
vector<string *> shuffleStrings(...) {
// ... (set things up) ...
vector<string *> shuffled(sz);
// ... (fill shuffled appropriately) ...
return shuffled;
}
But that is liable to be inefficient, because your program is then having to copy a load of stuff around. (It mightn't be so bad in this case, because a smallish array of pointers isn't very large and because C++ compilers are sometimes able to figure out what you're doing in cases like this and avoid the copying; the details aren't important right now.)
The other approach is to make the array not in shuffleStrings but in main; to pass a pointer to that array (or to its first element, which turns out to be kinda equivalent) into shuffleStrings; and to make shuffleStrings then modify the contents of the array.
void shuffleStrings(string * shuffled[], ...) {
// ... (set things up) ...
// ... (fill shuffled appropriately) ...
}
int main(...) {
// ...
string * shuffled[sz];
shuffleStrings(shuffled, theSt);
// output strings (main is probably a neater place for this
// than shuffleStrings)
}
Having said all this, the problems that are causing your symptoms lie elsewhere, inside shuffleStrings -- after all, main in your code never actually uses the pointer it gets back from shuffleStrings.
So what's actually wrong? I haven't figured out exactly what your shuffling code is trying to do, but that is where I bet the problem lies. You are making this array of pointers-to-string, and then you are filling in some of its elements -- the ones corresponding to numbers in randList. But if the numbers in randList don't cover the full range of valid indices in shuffled, you will leave some of those pointers uninitialized, and they might point absolutely anywhere, and then asking for their c_strs could give you all kinds of nonsense. I expect that's where the problem lies.
Your problem has nothing to do with any of the stuff you are saying. As you are a beginner I would suggest not presuming that your code is correct. Instead I would suggest removing parts that are not believed to be problematic until you have nothing left but the problem.
If you do this, you should quickly discover that you are writing to invalid memory.
part two : you can't seem to decide on the type of what you are returning. Are you building a pointer to an array to return or are you returning an array of pointers.... you seem to switch between these intermittently.
part three : read #Gareth's answer, he explains about passing parameters around nicely for your instance.

push attribute data to trie, add to multiple keys

my knowledge is limited but I have been working (hacking) at this specific data structure for awhile
I use a trie to store ontology strings that are then returned as a stack including the 'gap' proximity when get (string) is called. As an add on the trie stores attributes on the key. The further down the string the greater the detail of the attribute. This is working well for my purposes.
As an additional add on, I use a wildcard to apply an attribute to all child nodes. For example, to add 'paws' to all subnodes of 'mammals.dogs.' I push(mammals.dogs.*.paws). Now, all dogs have paws.
The problem is only the first dog get paws. The function works for push attributes without wild
If you want I can clean this up and give a simplified version, but in the past i've found on stackoverflow it is better to just give the code; I use 'z' as the '*' wild
void Trie::push(ParseT & packet)
{
if (root==NULL) AddFirstNode(); // condition 1: no nodes exist, should this be in wrapper
const string codeSoFar=packet.ID;
AddRecord(root, packet, codeSoFar); //condotion 2: nodes exist
}
void Trie::AddFirstNode(){ // run-once, initial condition of first node
nodeT *tempNode=new nodeT;
tempNode->attributes.planType=0;
tempNode->attributes.begin = 0;
tempNode->attributes.end = 0;
tempNode->attributes.alt_end = 0;
root=tempNode;
}
//add record to trie with mutal recursion through InsertNode
//record is entered to trie one char at a time, char is removed
//from record and function repeats until record is Null
void Trie::AddRecord(nodeT *w, ParseT &packet, string codeSoFar)
{
if (codeSoFar.empty()) {
//copy predecessor vector at level n, overwrites higher level vectors
if (!packet.predecessorTemp.empty())
w->attributes.predecessorTemp = packet.predecessorTemp;
return; //condition 0: record's last char
}
else { //keep parsing down record path
for (unsigned int i = 0; i < w->alpha.size(); i++) {
if (codeSoFar[0] == w->alpha[i].token_char || codeSoFar[0] == 'z') {
return AddRecord(w->alpha[i].next, packet, codeSoFar.substr(1)); // condition 2: char exists
}
}
InsertNode(w, packet, codeSoFar); //condition 3: no existing char --> mutal recursion
}
}
//AddRecord() helper function
void Trie::InsertNode(nodeT *w, ParseT &packet, string codeSoFar) // add new char to vector array
{
for (unsigned int i=0; i <=w->alpha.size(); i++) { // loop and insert tokens in sorted vector
if (i==w->alpha.size() || codeSoFar[0] < w->alpha[i].token_char) { //look for end of vector or indexical position
//create new TokenT
tokenT *tempChar=new tokenT;
tempChar->next=NULL;
tempChar->token_char=codeSoFar[0];
//create new nodeT
nodeT *tempLeaf=new nodeT;
tempLeaf->attributes.begin = 0;
tempLeaf->attributes.end = 0;
tempLeaf->attributes.planType = 0;
tempLeaf->attributes.alt_end = 0;
//last node
if (codeSoFar.size() == 1){
tempLeaf->attributes.predecessorTemp = packet.predecessorTemp;
}
//link TokenT with its nodeT
tempChar->next=tempLeaf;
AddRecord(tempLeaf, packet, codeSoFar.substr(1)); //mutual recursion --> add next char in record, if last char AddRecord will terminate
w->alpha.insert(w->alpha.begin()+i, *tempChar);
return;
}
}
}
root is global nodeT *w
struct ParseT {
string ID; //XML key
int begin = 0; //planned or actual start date
int end = 0; //planned or actual end date - if end is empty then assumed started but not compelted and flag with 9999 and
int alt_end = 0; //in case of started without completion 9999 case, then this holds expected end
int planType = 0; //actuals == 1, forecast == 2, planned == 3
map<string, string> aux;
vector<string> resourceTemp;
vector<string> predecessorTemp;
};
In this code
for (unsigned int i = 0; i < w->alpha.size(); i++) {
if (codeSoFar[0] == w->alpha[i].token_char || codeSoFar[0] == 'z') {
return AddRecord(w->alpha[i].next, packet, codeSoFar.substr(1)); // condition 2: char exists
}
}
you are returning as soon as you call AddRecord, even if it is because of a wildcard. It might be easier to have a separate loop when codeSoFar[0] == 'z' that goes through all the alphas and adds the record. Then have an else clause that does your current code.
Edit: Here is what I meant, in code form:
else { //keep parsing down record path
// Handle wildcards
if (codeSoFar[0] == 'z') {
for (unsigned int i = 0; i < w->alpha.size(); i++) {
AddRecord(w->alpha[i].next, packet, codeSoFar.substr(1)); // condition 2: char exists
}
}
else {
// Not a wildcard, look for a match
for (unsigned int i = 0; i < w->alpha.size(); i++) {
if (codeSoFar[0] == w->alpha[i].token_char) {
return AddRecord(w->alpha[i].next, packet, codeSoFar.substr(1)); // condition 2: char exists
}
}
InsertNode(w, packet, codeSoFar); //condition 3: no existing char --> mutal recursion
}
}

Indexing my while loop with count parameter in an array

My function takes an array of ifstream ofjects and the number of ifstream objects as seen below:
void MergeAndDisplay(ifstream files[], size_t count)
My problem is I want to use a while loop to read from the file(s) as long as one of them is open. When I get to eof, I close the file. So I thought I could do something like
int fileNum = 0;
while(files[fileNum].is_open() || something here) {
//do stuff
}
But I am not really sure how to put the correct amount of parameters in my while loop...
You will have to compute the logic of "is any file in this set open" separately. I suggest making it its own function so that the while loop can be clean and natural, e.g.
bool isAnyOpen(ifstream files[], size_t count) {
for (size_t i = 0; i < count; ++i) {
if (files[i].is_open()) return true;
}
return false;
}
Then you can write
while(isAnyOpen(files, count)) {
// Your code here
}
Edit: This is a more general case solution than what R Samuel Klatchko posted. If your problem is as simple as wanting to just read all the data out of all the files, then use his method since it is more direct.
You probably want
while (fileNum < count && files[fileNum].is_open())
with the condition that you increment fileNum whenever you open a new file in your loop.
Try something like this:
void ProcessStream(std::istream& input_file)
{
//...
}
// Your loop
bool a_file_is_open = true;
do
{
a_file_is_open = false;
for (unsigned int i = 0; i < MAX_FILES; ++i)
{
if (files[i].is_open())
{
a_file_is_open = true;
ProcessStream(files[i]);
break;
}
}
} while (a_file_is_open);