Advertisement

Frustrated!

Started by August 05, 2002 07:18 AM
1 comment, last by SteveBe 22 years, 3 months ago
I''m learning how to use structures, pointers to structures and passing an array of a structure as a pointer to a function. The program is boring but I''m trying to learn the principles. The program makes an array of the structure and inputs personal details. I then ask the user (me!) if any of the details need to be changed. If they do I pass the structure array to a function which changes the required field using a pointer. I need help!!! I have created a structure called perstruct. I have typedef''d it as follows:- typedef struct { char name[20]; int age; char sex; char location[30]; }perstruct; I have a function prototype as such: void detailchange(perstruct *personals, int userchoice); Userchoice holds the record number to be changed. After asking the user what record number needs to be changed the function is called as such detailchange (personals,userchoice); Entering the function the program then prints out each field for the selected record and then asks the user which one they would like to change. Here is where my problem occurs. I try to compile the program and the function contains errors that I cant work out. The compiler error is "base operand -> has non pointer type perstruct" Here is the function code that deals with changing the fields. switch (choice) { case 1: printf ("The current value is %s",personals[userchoice-1].name); printf ("Please enter the new value for name:"); gets (*personals[userchoice-1]->name); break; case 2: printf ("The current value is %d",personals[userchoice-1].age); printf ("Please enter the new value for age:"); scanf("%d%c",personals[userchoice-1]->age,&nl); break; case 3: printf ("The current value is %c",personals[userchoice-1].sex); printf ("Please enter the new value for sex:"); scanf ("%c%c",personals[userchoice-1]->sex,&nl); break; case 4: printf ("The current value is %s",personals[userchoice-1].location); printf ("Please enter the new value for location:"); gets (*personals[userchoice-1]->location); break; } Can someone tell me what I''m doing wrong?
Your problem is when using things like
personals[userchoice-1]->age
this only works if personals[userchoice-1] is a pointer, which it isn't because of the [userchoice-1]. Just change the -> to . in all the cases like this

That should solve your problem.

[edited by - grambo on August 5, 2002 8:36:14 AM]
--24 Beers in a Case.24 Hours in a Day.Coincedence? I think not!www.gramb0.co.uk
Advertisement
quote: Original post by SteveBe
printf ("Please enter the new value for location:");
gets (*personals[userchoice-1]->location);



As well as what the above poster said, change this to:


  printf ("Please enter the new value for location:");gets (personals[userchoice-1].location);  


Remember gets() takes a pointer to the beginning of an array of chars. As location is an array, you use the unadorned name to "get" the pointer to the beginning of the array. What you were doing was de-referencing this pointer which would yield the first character in the array. Which gets() doesnt want, it wants a pointer.

This topic is closed to new replies.

Advertisement