Pointer to a char* array

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
tatanka
 
Posts: 62
Joined: Tue Jul 03, 2012 9:06 am

Pointer to a char* array

Post by tatanka »

I have tried searching for this but can't seem to find any thing that helps.

What I have is

Code: Select all

#define NUM_DHTS (sizeof(dhtList)/sizeof(DHT*))
typedef char humStr[7];  //4 digits + decimal point + 1 decimal + null terminator
typedef char tempStr[7]; //4 digits + decimal point + 1 decimal + null terminator
humStr numhum[NUM_DHTS];
tempStr numtemp[NUM_DHTS];

typedef char* channel[8];
channel channelzero[8];
channel channelone[8];
channel channeltwo[8];
channel channelthree[8];

channel* channels[] = {&channelzero, &channelone, &channeltwo, &channelthree};
And this:

Code: Select all

int j=0;
  for (int i = 0; i <7; i ++)
  {
    channelzero[i] = numtemp[j];
    i += 1;
    channelzero[i] = numhum[j];
    j += 1;
  }
Is supposed to take the serial characters located in the numtemp and numhum arrays and load them into the channelzero array like this:

channelzero[8] = { "79F" , "28%" , "82F" , "33%" , "28F" , "10%" , "99F" , "77%"}

Now, what I am wanting to do is create a pointer that points to each of my 4 arrays so I can run a loop like this:

Code: Select all


for (int i = 0; i <4 ; i++)
    {
    char* chan[] = channel[i];
    for (int c = 0; c < 7 ; c ++)
    {
    strcat(transmission,fields[c]);
    strcat(transmission,chan[c]);
    }
    strcat(thingSpeakAPI, thingSpeakX);
    strcat(thingSpeakAPI, writeAPIKey[i]);
    Serial.println(transmission);
    Serial.println(thingSpeakAPI);
    updateThingSpeak(transmission, thingSpeakAPI);
    memset(transmission, '\0', 150);
    }
  }
I am receiving the error "cannot convert 'char*(*)[8][8]' to 'char*(*)[8]' in initialization. I'm pretty sure I'm using the typedef wrong because it compiled before I tried adding that. Let me know if you need any more information. Right now my code prints out to about 9 pages long, wasn't sure you need or even would want to see it.

Thank you in advance for your help.

Tim

User avatar
jcgoodman
 
Posts: 107
Joined: Thu Jan 23, 2014 6:03 pm

Re: Pointer to a char* array

Post by jcgoodman »

I don't think the error is happening in the code you quoted here, but I can tell you that once you find yourself declaring an array of pointers to pointers to chars, you should just go ahead and cancel your plans for the rest of the week.

How about just two bulk-storage arrays of floats for temperature and humidity, and maybe some int arrays to help keep track of which data is associated with each channel if you need that? Convert the floats into strings one at a time as you dump serial out.

User avatar
tatanka
 
Posts: 62
Joined: Tue Jul 03, 2012 9:06 am

Re: Pointer to a char* array

Post by tatanka »

All right then, that is kind of where I am at as well. I have two large arrays that hold all 20-40 values of temp and humidity, but what I am now having to do is parse them into 8 value groups in order to transmit them to the Thingspeak server. While it might be easier to group them into 8 values of temp in one transmission and 8 values of humidity in the next transmission, for a 20 value array, that would mean the fifth channel contains 4 temp and 4 humidity readings.

Thanks for the thought.

T

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: Pointer to a char* array

Post by adafruit_support_mike »

Two things:

1) When you work with pointers, typedef is your friend:

Code: Select all

typedef char* cString;
typedef cString* cList;
typedef cList* cTable;
typedef cTable* cDatabase;
2) '*' doesn't tell the compiler to allocate storage. '[]' does:

Code: Select all

cString str1 = "this is a string";  // only tells the compiler to allocate space for the pointer

cList wayWrong = { "string one", "string two", "string three" }; // Not Good At All

char str2[] = "this is another string";  // this is the correct way to do it:
cString correct[] = { str2 };  // char* is a pointer to memory allocated by 'char name[]'

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”