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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:30:55+00:00 2026-06-05T01:30:55+00:00

Assuming I have a structure such as: typedef struct { char * string1; char

  • 0

Assuming I have a structure such as:

typedef struct
{
    char * string1;
    char * string2;
} TWO_WORDS;

such that all the fields are of the same type, and my main has

TWO_WORDS tw;

can I reference string1 with tw[0] and string2 with two[1]? If so:

  • is this part of the c standard?
  • do i have to cast the struct to an array first?
  • what about fields which are different sizes in memory
  • what about fields which are different types but the same size?
  • can you do pointer arithmetic within a structure?
  • –

  • 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-05T01:30:57+00:00Added an answer on June 5, 2026 at 1:30 am

    I got pretty close with this construct:

    ((char**)&tw)[0];
    

    As an example:

    int main()
    {
        typedef struct
        {
            char * string1;
            char * string2;
        } TWO_WORDS;
    
        TWO_WORDS tw = {"Hello", "World"};
    
        printf("String1: %s\n", ((char**)&tw)[0]);
        printf("String2: %s\n", ((char**)&tw)[1]);
    
        return 0;
    }
    

    It is not guaranteed to work, as the compiler may add padding between fields. (Many compilers have a #pragma that will avoid padding of structs)

    To answer each of your questions:

    • is this part of the c standard? NO

    • do i have to cast the struct to an array first? YES

    • what about fields which are different sizes in memory
      This can be done with even more “evil” casting and pointer-math

    • what about fields which are different types but the same size?
      This can be done with even more “evil” casting and pointer-math

    • can you do pointer arithmetic within a structure?
      Yes (not guaranteed to always work as you might expect, but a structure is just a piece of memory that you can access with pointers and pointer-math)

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

Sidebar

Related Questions

Assuming I have a simple structure that looks like this: public class Range {
Assuming I have a list of objects that have the following fields parent value
So, given that I have some type of data structure like this within a
Assuming Visual C/C++ 6, I have a complex data structure of 22399 elements that
Assuming I have this HTML structure: <ul class='menu'> <li> <div></div> <div> <div></div> <div> <a
Assuming I have a struct: struct Vector { public int X, Y; // ...
Assuming you have only the URL to a file (hosted on the same server
alt text http://img517.imageshack.us/img517/8124/56267347.jpg Assuming i have a winform that has a button called Create
In my mvc 3 application, assuming I have a folder structure like \Views\Account\js\custom.js How
Assuming I have a tree structure UL --LI ---INPUT (checkbox) And I want to

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.