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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:57:15+00:00 2026-05-24T03:57:15+00:00

I have a structured defined as typedef struct{ char string1 char string2 int number1

  • 0

I have a structured defined as

typedef struct{
char string1
char string2
int number1
char string3
}structure1

and want to assign the values to string1,string2,number1,string3 in a loop like this

structure1 bob
for(int i = 0,i<=4,i++)
{
bob.i = assigned value
}

now I understand that code above in it’s generic form will only work for integers as you can’t just go string = string for assignment, but the same problem arises as I don’t know how to reference the values inside a struct without specifically naming them one by one. for strings there will be a second assignment relying on the i index to work out if it’s a integer or string at the time so it can perform the assignment. I was thinking something along the lines of enum’s but I’ve never used them in a practical sense before, just theoretical.

  • 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-24T03:57:16+00:00Added an answer on May 24, 2026 at 3:57 am

    It’s not possible in C. The closest to that would be calculating field offsets and then using them to assign values:

    int fieldOffset[4];
    
    structure1 base;
    
    fieldOffset[0] = (char*)&base.string1 - (char*)&base;
    fieldOffset[1] = (char*)&base.string2 - (char*)&base;
    fieldOffset[2] = (char*)&base.number1 - (char*)&base;
    fieldOffset[3] = (char*)&base.string3 - (char*)&base;
    
    structure1 structYouWantToAssign;
    
    for (int i = 0; i < 4; ++i)
    {
        *((char*)&structYouWantToAssign + fieldOffset[i]) = assignedValue;
    }
    

    Warning: this code is just to demonstrate it is possible to assign fields without their name, but you should not use it!

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

Sidebar

Related Questions

I have a text string structured like this: = Some Heading (1) Some text
I have a piece of XML that is structured similar to this: <root> <score
I have to read invoice ascii files that are structured in a really convoluted
I have 2 tables: posts tags Tags table is structured like this: post_id tag
Our application is well structured (well we did our best!) and we have split
Okay, this is just a crazy idea I have. Stack Overflow looks very structured
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 have a structure which I need to populate and write to disk (several
I have a structure like this: <ul> <li>text1</li> <li>text2</li> <li>text3</li> </ul> How do I

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.