473,769 Members | 6,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get struct members address though a "pointer to struct"

5 New Member
Hi,

Please have a look on the next code :

struct foo{
float b;
char z[5];
};
struct foo foo_init = {234.77,"xcdy"} ;
struct foo *prt_prt=&foo_i nit;

Q: How can I have access to the struct members in the same way as I got access to an array like:

int a[4] = {1,2,3,4,5}
int *p=&a[0]

printf ("address of a[1] is %p", (p+1))

??
Thanks
Dec 11 '07 #1
4 10622
jthep
34 New Member
declare a pointer and point it to a struct:

struct foo *f1;
struct foo f2;

f1 = &f2;
to access:
f1 -> b = 2.5;
f1 -> z[0] = 'f';

I'm guessing foo_init and prt_prt are global variables so accessing them would be the same as above:

prt_prt -> b to access the first member
prt_prt -> z to acces the array

or to put it another way (*prt_prt).b & (*prt_prt).z. Using the '->' operator is cleaner.
Dec 11 '07 #2
MetaKith
5 New Member
Thanks for the answer but what i needed was something else:

( I don't know to explain exactly the problem, it might be impossibele though)

so:
I want to get the address of an struct member without knowing the actual name of the member. In the expresion *ptr->member (here I know the name) so I can get the address and the value very easely but how can I get this infos without knowing the name of the member?


Sorry for my poor language

Thanks in advance
Dec 11 '07 #3
scruggsy
147 New Member
Thanks for the answer but what i needed was something else:

( I don't know to explain exactly the problem, it might be impossibele though)

so:
I want to get the address of an struct member without knowing the actual name of the member. In the expresion *ptr->member (here I know the name) so I can get the address and the value very easely but how can I get this infos without knowing the name of the member?


Sorry for my poor language

Thanks in advance
structs are heterogenous, arrays are homogenous. So you need to know at least something about the layout of the struct in order to access its elements.
If you don't know the name of a member, but you know that the struct has a layout like {float b; char z[5]} and you know that your environment uses 32-bit floats, you can do something like:
Expand|Select|Wrap|Line Numbers
  1. char getCharFromStruct(void* myStruct, int index)
  2. {
  3.   char* ch = ((char*)myStruct) + 4;  //skip the float
  4.   ch += index;  //find z[index];
  5.   return *ch;
  6. }
  7.  
But that is not very clean programming and 99.9% of the time there's no good reason to do something like that and many good reasons not to.
Dec 11 '07 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
But that is not very clean programming and 99.9%
And... it may not work at all due to compiler alignment of struct members with word boundaries.

I suggest you research how to set up a thunk table since that sounds like what you want.
Dec 11 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

23
6635
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
3
20855
by: signuts | last post by:
I am wondering what it means when a pointer is aligned? Could someone perhaps enlighten me or point me in the right direction? Thank you in advance. -- Sig
188
17433
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
10
3519
by: Rick Anderson | last post by:
All, I am receiving the following compilation error on LINUX (but not Solaris, HPUX, WIN32, etc): compiling osr.c LBFO.h(369): warning #64: declaration does not declare anything extern struct foobar; ^
3
2488
by: IR | last post by:
Hi, I've been trying to do the following (which doesn't compile) : template<class T, class F = Example<T struct Example { F foo(); };
8
40471
by: cman | last post by:
What does this kind of typedef accomplish? typedef struct { unsigned long pte_low; } pte_t; typedef struct { unsigned long pgd; } pgd_t; typedef struct { unsigned long pgprot; } pgprot_t I am familiar with "typedef int NUMBER", but how does it work with structures. Tilak
5
2594
by: Gianni Mariani | last post by:
I'm hoping someone can tell me why using member address of works and why using the dot operator does not in the code below. The code below uses the template function resolution mechanism to determine wether a class contains a member. My understanding is that if a template function has an error during resolution of the function types, it is quietly eliminated from the resolution process. This can be used to detect things that would...
3
2352
by: Mahesh | last post by:
#include <stdio.h> #include <string.h> #include <malloc.h> int main(void) { int key = 120; char *s = "String to Encrypt using XOR"; char *ss = s; char echar; char dchar;
18
2460
by: Stephan Beal | last post by:
Hi, all! Before i ask my question, i want to clarify that my question is not about the code i will show, but about what the C Standard says should happen. A week or so ago it occurred to me that one can implement a very basic form of subclassing in C (the gurus certainly already know this, but it was news to me). What i've done (shown below) seems to work all fine and well, and does exactly what i'd expect, but i'm asking about
0
9424
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10051
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10000
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8879
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.