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 6171005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:08:16+00:00 2026-05-23T23:08:16+00:00

I need to store two items per array element — two arrays of char

  • 0

I need to store two items per array element — two arrays of char, which might contain null bytes — and then still be able to use sizeof() to get their length. Since these values will not change during execution, I think GCC should be able to handle this.

Here’s the code:

#include <stdlib.h>
#include <stdio.h>

struct name_data {
    char *name;
    char *data;
} name_bins [] = {
    { "John", "\xAA\xAA\x00\xAA" },
    { "Mark", "\xFF\x0A\x00\x33\x01\x01\x03\x04\x04\x05" },
};

char bin_test[] = "\xFF\x0A\x00\x33\x01\x01\x03\x04\x04\x05";

int main () {
    printf("sizeof(bin_test) = %lu\n", sizeof(bin_test));
    printf("sizeof(name_bins[1].data) = %lu\n", sizeof(name_bins[1].data));
    exit(0);
}

The output of this code is:

sizeof(bin_test) = 11
sizeof(name_bins[1].data) = 8

However, bin_test is equivalent to name_bins[1].data in content — although the type definition is different — bin_test is a char[] and names_bins[1].data is a char*.

Is there a way to define the name_bins array with char[]s instead?
Is there a way to force GCC to recognize this values as static constants and return the real content size with sizeof() — which it already calculates at compile time?

  • 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-05-23T23:08:18+00:00Added an answer on May 23, 2026 at 11:08 pm

    You can almost do what you want by storing the size of data as a separate entry:

    struct name_data {
        char  *name;
        char  *data;
        size_t data_size;
    } name_bins[] = {
        {
            "John",
            "\xAA\xAA\x00\xAA",
            sizeof("\xAA\xAA\x00\xAA")
         }, {
             "Mark",
             "\xFF\x0A\x00\x33\x01\x01\x03\x04\x04\x05",
             sizeof("\xFF\x0A\x00\x33\x01\x01\x03\x04\x04\x05")
         }
    };
    

    And then:

    printf("sizeof(bin_test) = %lu\n", sizeof(bin_test));
    printf("sizeof(name_bins[1].data) = %lu\n", (unsigned long)name_bins[1].data_size);
    

    Then you’d just have to make sure your name_bins initialization was right. You could toss a macro in the mix to avoid repeating yourself though:

    #define BIN(x,y) { (x), (y), sizeof(y) }
    
    struct name_data {
        char  *name;
        char  *data;
        size_t data_size;
    } name_bins [] = {
        BIN("John", "\xAA\xAA\x00\xAA"),
        BIN("Mark", "\xFF\x0A\x00\x33\x01\x01\x03\x04\x04\x05")
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to store phone numbers in a table. Please suggest which datatype should
I need to store items with a calendar date (just the day, no time)
I need to be able to store a large list of ordered items in
I need to store two types of objects, Feed and Folder , in an
I have two table TempStore and Store with the same column called Items. There
I need to store few items and its properties in form of a key
I'm trying to compare four arrays. Two of the four arrays store filenames, while
I have a UINavigationBar that has two button items already. I will need to
I have id values for products that I need store. Right now they are
I need to store products for an e-commerce solution in a database. Each product

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.