Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8147929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:31:13+00:00 2026-06-06T14:31:13+00:00

I have a structure: typedef struct stock { const char* key1p2; // stock code

  • 0

I have a structure:

typedef struct stock {
  const char* key1p2;    // stock code
  const char* key2p2;    // short desc
  const char* desc1;     // description
  const char* prod_grp;  // product group
  const char dp_inqty;   // decimal places in quantity
  const long salprc_u;   // VAT excl price
  const long salprc_e;   // VAT includive price
  const long b_each;     // quantity in stock
  const long b_alloc;    // allocated qty
  const char* smsgr_id;  // subgroup
  const char** barcodes; // barcodes
} stock_t;

and I want to initialise arrays of instances of this structure, in one line of code per stock struct.

I have tried:

stock_t data_stock[] = {
    { "0001", "Soup",  "Tomato Soup",     "71", 0, 100, 120, 10, 0, "",   {"12345", "23456", NULL} },
    { "0002", "Melon", "Melon and Ham",   "71", 0, 200, 240, 10, 0, "",   {"34567", "45678", NULL} },
    ...
    { NULL,   NULL,    NULL,              NULL, 0,   0,   0,  0, 0, NULL, NULL         }
};

but it fails with:

data.c:26:74: warning: incompatible pointer types initializing 'const char **' with an expression of type 'char [6]'
      [-Wincompatible-pointer-types]
  { "0001", "Soup",  "Tomato Soup",     "71", 0, 100, 120, 10, 0, "",   {"12345", "23456", NULL} },
                                                                         ^~~~~~~

It is the barcode field which is problematic, as it is char**.

(that was clang, but GCC reports a similar error, but less helpfully.)

It’s almost as if the compiler has ignored the curly brace before “12345”.

I can work around the problem using:

const char *barcodes0001[] = {"12345", "23456", NULL};

stock_t data_stock[] = {
  { "0001", "Soup",  "Tomato Soup",     "71", 0, 100, 120, 10, 0, "",   barcodes0001 },

Is the cause of this problem a different between char [] and char *, or is there something more subtle. (Perhaps you can initialise arrays of structs, but not structs of arrays.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:31:15+00:00Added an answer on June 6, 2026 at 2:31 pm

    To avoid to have to declare a named dummy variable you can use a compound literal

    stock_t data_stock[] = {
        { "0001", "Soup",  "Tomato Soup",     "71", 0, 100, 120, 10, 0, "",   (const char*[]){"12345", "23456", NULL} },
        { "0002", "Melon", "Melon and Ham",   "71", 0, 200, 240, 10, 0, "",   (const char*[]){"34567", "45678", NULL} },
        ...
        { NULL,   NULL,    NULL,              NULL, 0,   0,   0,  0, 0, NULL, NULL         }
    };
    

    This is a way to define a local temporary variable with a syntax of (type){ initializers } which is available since c99 (and clang and gcc have them).

    Edit: The lifetime of these compound literals is the same as your variable data_stock so I suppose this is ok. In any case I think you should mark most of your fields also as const, e.g const char*const key1p2;

    Also it would be much easier to read if you’d use designated initializers something like

    { .keyp1 = "0001", ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a structure typedef struct store { char name[11]; int age; } store;
I have such a C++ structure: typedef struct _FILE_OP_BLOCK { unsigned short fid; //
I have the structure typedef struct EData { int a; char c; } Edata
I have this structure: typedef struct { int data[10]; } small_structure; and this code:
For example, I have this structure and code: typedef struct { long long *number;
I have the following structure typedef struct DeviceInfo { char[30] name; char[30] serial Number;
I have this structure: typedef struct { char *str; ... } nameType; I create
If I have a structure such as typedef struct _people { char *name; bool
Assuming I have a structure such as: typedef struct { char * string1; char
I am very beginner in C. I have the following structure typedef struct {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.