Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I increment the pointer to an arrayed structure? 3

Status
Not open for further replies.

TheKeeya

Programmer
May 7, 2003
6
0
0
US
I have a structure that is an array (struct tag my_struct[10]). I am trying to pass a pointer to that structure to a function (void myfunction(struct tag *p)). Inside the function I try to reference the pointer to the structure. It works well for (p->.value) which points to mystruct[0].value, but I can not figure how to access the other array values (such as my_struct[1].value or my_struct[2].value, because I get an error if I attempt to use p[1]-> value = 1 or *(p+1)-> value =1, etc and the like.

code sample:

#include <stdio.h>
#include <string.h>

struct tag{
char suit;
int value;
};

struct tag my_struct[5];
void shuf(struct tag *p);

int main(void)
{
shuf(my_struct);
printf(&quot; %d : %c \n&quot; , my_struct[0].value, my_struct[0].suit);
printf(&quot; %d : %c \n&quot; , my_struct[1].value, my_struct[1].suit);
return (0);
}

void shuf(struct tag *p)
{
p-> suit = 'h'; /* works: assess my_struct[1]*/
p-> value = 18; /* same */

p[1]-> suit = 'w'; /* gets an error */

}

Error: E2288 - pointer to structure required on left side of -> or ->* in function shuf(tag *).

Does anyone know of a way to do what I'm attempting to do.
 
I get an error if I attempt to use p[1]-> value = 1 or *(p+1)-> value =1, etc and the like.

Code:
(p+1)->value = 1;



-pete
[sub]I just can't seem to get back my IntelliSense[/sub]
 
It starts life as an array, so treat it as an array
p[0].suit = 'h';
p[1].value = 42;

 
you dont increment the pointer in the shuf function. since
this is the bare structure and not the array. you will increment the array pointer. so you might pass the pointer to the array instead of the structure if you must increment the array pointer.

tomcruz.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top