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

  • Home
  • SEARCH
  • 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 structure defined like so: typedef struct { int n; int *n_p;
I have a structure defined as struct xyz { char *str; int x; int
I have a simple structure that is defined like so: typedef struct { int
I have this struct defined: typedef struct Socket_s { ... SocketConnectFunc Connect; ... }Socket;
I have the following struct in C++: #define MAXCHARS 15 typedef struct { char
I have a data structure defined as struct myDataStruct { int32_t header; int16_t data[8];
I have a NumPy array [1,2,3,4,5,6,7,8,9,10,11,12,13,14] and want to have an array structured like
I have a structure I would like to optimize the footprint of. typedef struct
Consider this piece of code: struct Trade { float Price; char* time; int shares;
I have the following data structure in C: typedef struct { void* buffer; ...

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.