473,219 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,219 software developers and data experts.

Dereferencing of structure pointers

Hi,
I have a pointer to structure, and i have to check which member has a desired value.
For that can any one suggest me the best way to access the members while checking for the desired value in that member.

For example ,if i have an array a[]={0,1,2,3};
here say my desired vlue is 2
then i can check
while(--a == 2)
{

break;
}
return(i);

My question is best way to do that when we have a structure instead of array.
Jul 29 '06 #1
2 4297
dna5122
12
I'm not sure I understand the point of doing that with a structure. A structure has different kinds of data types, an array does not.

Besides, the example you gave won't do anything. It's also VERY unsafe code that should never, ever, be written into a program.
Jul 29 '06 #2
Hi,
I have a pointer to structure, and i have to check which member has a desired value.
For that can any one suggest me the best way to access the members while checking for the desired value in that member.

For example ,if i have an array a[]={0,1,2,3};
here say my desired vlue is 2
then i can check
while(--a == 2)
{

break;
}
return(i);

My question is best way to do that when we have a structure instead of array.
Hi,
First thing is that the way you are trying to decrement the address of the array will throw an error of L-value becoz you cannot decrement the address rather you have to collect its address in a pointer and then decrement it.

Now coming to your question :

There are two ways to know the value of the members of a structure through pointer.

1. If your structure has all the members of same data types then you can easily access the members through pointers but structures are meant to hold diff data types so this method will not help u much.

2. If you have diff data types in the structure then you must be aware of the data types of the members before accessing their values. Their values can be accessed in the following way.

struct base x;

unsigned char *ptr, arr[sizeof(x)];

unsigned long *p;

ptr = (char*)&x;

memcpy(arr, ptr, sizeof(x));

p = (unsigned long*)arr;

printf("%lu\n\n", *p);

In the above code I have assumed that the first member of the structure is a unsigned long variable. U can type cast the address of the array as per the data type of the member variable and fetch the value.
Jul 31 '06 #3

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

Similar topics

2
by: Matthias | last post by:
Hi, again, I have a problem using containers of pointers (I'm near to the point to drop them alltogether and work on usual containers instead...). The problem is, if I want to perform some...
5
by: Angel Tsankov | last post by:
Does the standard define what happens when a NULL pointer is dereferenced? If so, where?
28
by: Martin Jørgensen | last post by:
Hi, I have a "funny" question, which I think is pretty "healthy" to examine... This program is being investigated: - - - - - - - #include <iostream> using namespace std; #define DAYS 7
9
by: Cyron | last post by:
Hello friends, Recently I have begun exploring the features that the STL map collection provides. While learning how it worked, I encountered a result that confused me. Up until now, I had...
8
by: friend.05 | last post by:
I have three files. graph.h: My header file where I have typedef for my structure and function delcaration typedef struct _ipc_actors ipc_graph_actors; typedef ipc_graph_type...
4
by: Pritam | last post by:
line 7: error: dereferencing pointer to incomplete type 1. #include<stdio.h> 2. #include<sys/stat.h> 3. #include<stdlib.h> 4. void execname() { 5. struct task_struct *my; 6. my =...
20
by: prashant.khade1623 | last post by:
I am not getting the exact idea. Can you please explain me with an example. Thanks
12
by: viza | last post by:
Hi I have program1.c: typedef int (*fn_t)(int); int fn( int f ){ return f; }
3
by: sritejv | last post by:
Hello everyone, I am having a problem with typecasting void pointers.I have read the pointer basics but still cant understand why the following test code doesnt work. void *xyz; struct abcd...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.