problem with array of pointers

Hi every body.

I’m here because i haven’t find a solution for my probleme.
I’ve created a struct and i would like to have a array of this struct.
I try differents possibilities and i’ve tried this but without success.

The code compile successfully but when i would like to print my spots, they aren’t all in the array… I think, i make a fault of memory.
My IDE indicate a strange error.

Erreur: impossible d’assigner une valeur de type “spot**” a une entite de type “spot **”

this is a extract of my code:

struct spot {
	String name;
	int distance;
};

typedef struct spot spot;


spot **g_spots_gps;

void setup()
{
       String g_name_file_gps=PTS.CSV;
       float Pi=3.14;
       int g_number_line_sd=MySD_countNumberLine();

       g_spots_gps = (spot**)malloc(g_number_line_sd*sizeof(spot*));

       MySD_addSpot(g_name_file_gps, g_spots_gps,Pi);
       spot_printValue(g_spots_gps);
}

the result is that my program print only five spots out of ten…

Thanks in advance.

Hi and welcome to the forum,

The code you have provided is incomplete (only a portion/snippet of your code). Please post (or attach it, if it's too large) the complete code so that people can review it.

Please remember to use the "auto format feature" of the IDE before copying/pasting your code. (CTRL-T, in the IDE)

Thanks!

dan

This:

spot **g_spots_gps;

declares a pointer to a pointer to a struct spot. Probably not what you want.

Instead, use

spot *g_spots_gps;

which declares a pointer to a struct spot.

Now this:

g_spots_gps = (spot*)malloc(g_number_line_sd*sizeof(spot));

gives you a pointer to a block of RAM big enough to hold g_number_line_sd struct spots.

Thanks to the c pointer syntax, you can treat that pointer as a pointer, or as an array. For example:

g_spot_gps[0].name="first spot";
g_spots_gps[0].distance=1;
g_spots_gps[1].name="second spot";
g_spots_gps[1].distance=2;
...
g_spots_gps[9].name="tenth spot";
g_spots_gps[9].distance=10;

or, equivalently, using pointer syntax:

g_spots_gps->name="first spot";
g_spots_gps->distance=1;
(g_spots_gps + 1)->name="second spot";
(g_spots_gps + 1)->distance=2;
...
(g_spots_gps + 9)->name="tenth spot";
(g_spots_gps + 9)->distance=10;

Regards, Ray L.

Array arithmetic is one of the most important things that a C programmer needs to "grok".

Other thing I would tend to do is move spot_printValue into the struct as a method.

hi. I have tried the code of RayLivingston. I've the same problem.

Just for test I stop to developp on my IDE, and i return on Arduino interface and It's ok.....

So i will developp on arduino interface... thank for your contribution every body.