pointer to array of strings

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
typedef struct
{
    const char *title;
    const WHAT items;
    const uint8_t itemsCount;
} myType;

myType a =
{
    "AVR FREAKS",
    WHAT,
    2
}

doSomething(a.items[1]);

"items" should point to a constant array of constant strings. For example, an array which contains "Hello!" and "How are you?". So "doSomething()" will get "How are you?" as parameter.

I tried many pointer configurations but always get an error or "initialization from incompatible pointer type [-Wincompatible-pointer-types]" warning.

Please save me from pointer headache! crying

This topic has a solution.

Slow and Steady!

Last Edited: Fri. Oct 16, 2020 - 09:58 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not enough context. (for example what is "WHAT" ?)

 

But if this about const pointers to const data remember there are three ways to make const pointers:

const type * p1;
type * const p2;
const type * const p3;

p1 is a pointer to an item of type that is const

p2 is a const pointer to an item of type

p3 is a const pointer to an item of type that is const.

 

Or is the question really about what the implied type of literal string constants are? Like exactly what type is "AVR Freaks" ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is a standard 'C' question, and it's quite common to have arrays of strings.

 

So have you tried a 'C' textbook? or just google "pointer to array of strings" ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

what is "WHAT" ?

I wish I'd known! This is my question.

 

clawson wrote:

Or is the question really about what the implied type of literal string constants are? Like exactly what type is "AVR Freaks" ?

Luckily I know this one. "AVR FREAKS" is a null-terminated constant char array. smiley

 

awneil wrote:

So have you tried a 'C' textbook? or just google "pointer to array of strings" ...

I tried. They say: "This is your array. So assign pointer to &array. Easy as a pie!"

But I have to implement pointer type manually.

Oh; as usual, I forgot to mention something. blush

String constants may (if needed) be limited to 19 characters (excluding final '\0').

Slow and Steady!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
p1 is a pointer to an item of type that is const

p2 is a const pointer to an item of type

p3 is a const pointer to an item of type that is const.

 

See: http://c-faq.com/decl/constptrco...

 

pajuhesh80 wrote:
assign pointer to &array.

No, I'm sure they don't say that.

 

See: http://c-faq.com/aryptr/index.html

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

So have you tried a 'C' textbook? or just google "pointer to array of strings" ...

pajuhesh80 wrote:
I tried. They say: "This is your array. So assign pointer to &array. Easy as a pie!"

But I have to implement pointer type manually.

Go on.

 

Very first hit in that google search: https://overiq.com/c-programming-101/array-of-pointers-to-strings-in-c

char *sports[] = {
                     "golf",
                     "hockey",
                     "football",
                     "cricket",
                     "shooting"
                 };

isn't that exactly what you want?

 

Again, this is standard 'C' stuff , so you can easily experiment with it on a PC - much easier than on a small microcontroller!

 

Use the PC debugger to see the pointer, the pointer value, and what the pointer points to ...

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Oct 13, 2020 - 11:25 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Probably what you are looking for-

 

https://godbolt.org/z/hv4PjK

 

Your WHAT is a [const char** const]- a pointer to a (constant char pointer) that is constant. The added const just prevents modifying the pointer to the const char*, but you probably never get that far (but doesn't hurt).

 

There is a lot of const going on, and some of it redundant. With some experimenting, you can figure out the details of what is going on and where the const may be required/wanted/not wanted.

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not much different to curtvm example,  i would tend to define the struct without the members themselves as const (just what the pointer members are pointing to), but then you can make myType a itself const

const char * const what[] = {"hello", "how are you"};

typedef struct
{
    const char *title;
    const char * const *items;
    uint8_t count;
} myType;

const myType a =
{
    "AVR FREAKS",
    what,
    sizeof(what) / sizeof(what[0])
};

int main(void)
{
    printf("a.title %s\n", a.title);
    for (uint8_t i = 0; i < a.count; i++)
    {
        printf("a.item[%d] %s\n", i, a.items[i]);
    }
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

If they are "strings" can I suggest __flash/PROGMEM ? (no point wasting RAM with these things and unlike ARM (and, admittedly, the most recent AVRs) just "const" does not necessarily guarantee "only hold in flash")

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think this is more or less what OP wants:

 

#include<stdint.h>

#include<stdio.h>

 

typedef const char * const str_array[];

 

//typedef str_array* str_array_ptr; // pointer type easy way

typedef const char * const (* str_array_ptr)[]; // pointer type hard way

 

typedef struct

{

  const char *title;

  const str_array_ptr items;

  const uint8_t itemsCount;

} myType;

 

str_array my_items = {"item_1", "item_2", "item_3", "item_4"};

 

myType a =

{

  "AVR FREAKS",

  &my_items,

  4

};

 

int main(int argc, char *argv[]) {

  puts(a.title);

  for (int i = 0; i < a.itemsCount; i++) {

    puts((*(a.items))[i]); // Sorry for so many parenthesis, but I can't be bothered to learn precedence order between . * and []

  }

  return 0;

}

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Finally, I did it this way:

typedef struct
{
	const char *title;\
	const char ** items;
	const uint8_t itemsCount;
} myType;

const char* _items[] = { "Hello!", "How are you?" };

myType _start =
{
	"AVR FREAKS",
	_items,
	2
};

doSomething(myType.Items[1]);

Slow and Steady!

Last Edited: Fri. Oct 16, 2020 - 09:43 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

note that names beginning with an underscore are reserved for system use - you should not be naming your own symbols with a leading underscore.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

note that names beginning with an underscore are reserved for system use - you should not be naming your own symbols with a leading underscore.

Good point. Thanks.

Slow and Steady!