473,402 Members | 2,064 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,402 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 4301
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.