Pages: [1] 2   Go Down
Author Topic: invalid conversion from char*() to char *  (Read 1573 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a sensor type const stored in the flash,  then I make a sensor struct, which includes a type variable, I don't want to store type for each sensor, since many sensors(temperature sensor) can have the same value on the type. so what I want to do is to make the type as a pointer of char, and this pointer points to the const in the flash, such as   type_temp[] PROGMEM.
 

prog_char type_temp[] PROGMEM = "sensors.temperature";   

struct sensor{                     /* the structure type */
    char id[20];             /* id */
    char *type;             /* type */
    float value;                 /* e.g. 12.75 per hour */
}sensor_list[4];

However, when I tried the initialization of sensor struct

sensor_list[0] = (sensor){"1",&type_temp,"111",3};
// want to assign the address of type_temp(in the flash) to the type pointer in the sensor structure,

a error is thrown out saying 'cannot convert prog_char (*)[43] to 'char * ' in initialization '
even I change  char *type to prog_char  *type;   still don't work
what can I do?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 480
Posts: 18720
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

However, when I tried the initialization of sensor struct

Code:
sensor_list[0] = (sensor){"1",&type_temp,"111",3};

a error is thrown out ...

This is an assignment, not an initialization. You don't initialize an array like this.
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49027
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also keep in mind that you do not have a type named "sensor". You have a type named "struct sensor".

sensor_list[0].id = strcat("1");
sensor_list[0].type = ??; // You get to figure this one out
sensor_list[0].value = ??; // This one, too.

Why do you have 4 values in the list to try to initialize the 3 members of the structure? Why are you trying to value the float one with "111"?

Time to buy a clue, I think.
Logged

Offline Offline
Jr. Member
**
Karma: 4
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi helxsz,
I would do the following:

Code:
prog_char type_temp[] PROGMEM = "sensors.temperature";   

struct sensor{    /* the structure type */
    char id[20];  /* id */
    char *type;   /* type */
    float value;  /* e.g. 12.75 per hour */
}sensor_list[4];

void setup()
{
  strcpy( sensor_list[0].id, "1");
  sensor_list[0].type = type_temp;
  sensor_list[0].value = 12.75;
  // ... continue with sensor_list[1], sensor_list[2], etc.
}

Quote
Also keep in mind that you do not have a type named "sensor". You have a type named "struct sensor".

In C++
Code:
struct Sensor { .... };
defines a type named Sensor, in fact you can define variables in this way:

Code:
Sensor s1;

 
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26194
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
struct sensor{                     /* the structure type */
    char id[20];             /* id */
    char *type;             /* type */
    float value;                 /* e.g. 12.75 per hour */
}sensor_list[4] = { {"1",&type_temp,"111",3},
                         {...},
                         {...},
                         {...};
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think you cannot mix pointers to progmem and sram. You have to use special functions to read progmem-stored bytes into sram. Leave out progmem or use the F() macro.

My 2 cents.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26194
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I think you cannot mix pointers to progmem and sram. Y
You can mix them freely, but you need to be careful how you dereference them.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I think you cannot mix pointers to progmem and sram. Y
You can mix them freely, but you need to be careful how you dereference them.

I thought PROGMEM addresses were meaningless in SRAM context and vice-versa. Can you explain what you mean exactly ? Thank you smiley
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26194
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I thought PROGMEM addresses were meaningless in SRAM context and vice-versa
Addresses (pointer) are simply numbers. How you interpret them is what is important.
If you know an address is in PROGMEM, then you dereference it using a PROGMEM-appropriate method.
If you know an address is in RAM, then you dereference it using a RAM-appropriate method.

However, you can't tell simply by examination of the value of the pointer whether it points to RAM or flash.

Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Still I don't get how a char* can point to a PROGMEM string... Or maybe by "mixing them freely" you meant a different thing ?

Can you provide some a code example ?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26194
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Still I don't get how a char* can point to a PROGMEM string..
A pointer is simply a variable that contains the address of the entity.
The address itself is simply a number - there's nothing special about it, but it cannot contain any information about which address space (RAM or flash) it points to - that has to be known in advance.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Mixing PROGMEM and SRAM pointer is therefore _not_ possible. A char* can't point to a PROGMEM char array. Is this correct ? Anyway I'll do some experiments ASAP...
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49027
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
A char* can't point to a PROGMEM char array. Is this correct ?
No, it is not correct. A char pointer can point to SRAM or to PROGMEM. But, YOU must know where it is pointing, and use the proper methods to retrieve the pointed to data (or set it).
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You mean like:

Code:
prog_char pgm_string[] PROGMEM = "Hallo, world!";

const char* ptr = pgm_string;

which compiles, but produces nothing if one tries to println(ptr).
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49027
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You mean like:
Yes.

Quote
which compiles, but produces nothing if one tries to println(ptr).
Of course not. You are treating ptr as though it pointed to SRAM. It doesn't, so you need to use the appropriate pointer-to-progmem functions to get the data being pointed to.
Logged

Pages: [1] 2   Go Up
Jump to: