468,513 Members | 1,772 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

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 4102
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

Post your reply

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

Similar topics

2 posts views Thread by Matthias | last post: by
5 posts views Thread by Angel Tsankov | last post: by
28 posts views Thread by Martin Jørgensen | last post: by
9 posts views Thread by Cyron | last post: by
8 posts views Thread by friend.05 | last post: by
20 posts views Thread by prashant.khade1623 | last post: by
12 posts views Thread by viza | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.