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

The Archive Base Latest Questions

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

I have a structure to represent strings in memory looking like this: typedef struct

  • 0

I have a structure to represent strings in memory looking like this:

typedef struct {
    size_t l;
    char   *s;
} str_t;

I believe using size_t makes sense for specifying the length of a char string. I’d also like to print this string using printf("%.*s\n", str.l, str.s). However, the * precision expects an int argument, not size_t. I haven’t been able to find anything relevant about this. Is there someway to use this structure correctly, without a cast to int in the printf() call?

  • 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-26T17:16:24+00:00Added an answer on May 26, 2026 at 5:16 pm

    You could do a macro

    #define STR2(STR) (int const){ (STR).l }, (char const*const){ (STR).s }
    

    and then use this as printf("%.*s\n", STR2(str)).

    Beware that this evaluates STR twice, so be carefull with side effects, but you probably knew that already.

    Edit:

    I am using compound initializers such that these are implicit conversions. If things go wrong there are more chances that the compiler will warn you than with an explicit cast.

    E.g if STR has a field .l that is a pointer and you’d only put a cast to int, all compilers would happily convert that pointer to int. Similar for the .s field this really has to correspond to a char* or something compatible, otherwise you’d see a warning or error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data structure that represents C# code like this: class Namespace: string
I have a structure like this: <ul> <li>text1</li> <li>text2</li> <li>text3</li> </ul> How do I
I have a structure like this.... UITableViewController -> UITableViewCell -> UIView I need the
I have XML like this: <type> <mainType>...</mainType> <subtype>...<subtype> </type> The mainTypes are restricted xs:strings
I have a structure named WaveSize to represent both an amount of samples or
I have a sort of tree structure that represent a hierarchy of layers in
I have a structure in C#: public struct UserInfo { public string str1 {
I have a structure: struct pkt_ { double x; double y; double alfa; double
I am looking for an efficient data structure to represent a priority list. Specifically
I have a JTable which is loaded from a data-structure using table model.The data-structure

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.