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
4 10622
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.
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
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: - char getCharFromStruct(void* myStruct, int index)
-
{
-
char* ch = ((char*)myStruct) + 4; //skip the float
-
ch += index; //find z[index];
-
return *ch;
-
}
-
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Hans |
last post by:
Hello,
Why all C/C++ guys write:
const char* str = "Hello";
or
const char str = "Hello";
|
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
|
by: infobahn |
last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
|
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;
^
|
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();
};
| |
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
|
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...
|
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;
|
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
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |