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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi, i just can`t seem to get the fo

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi, i just can`t seem to get the following code to compile properly.
these are the errors i get...any help would be greatly appreciated :=)
error C2228: left of '.identity' must have class/struct/union type
error C2228: left of '.name' must have class/struct/union type


#include <stdio.h>
#include <string.h>
#define MAX 10

typedef struct {
char name[20];
int identity;
}GOLFER;

void initial_screen(void);
void get_details(GOLFER *ptr);
void print_details(GOLFER player);

void main (void)
{
GOLFER player[MAX];
initial_screen();
get_details(&player[MAX]);
print_details(player[MAX]);

}


void initial_screen(void)
{
printf(&quot;\t*************************************************\n&quot;);
(&quot;\n\t*************************************************\n&quot;);
puts(&quot;\n\t&quot;);
puts(&quot;\tInput Players or (x) to finish input&quot;);
}


void get_details(GOLFER *ptr)
{

int i;
for(i=1;i<=10;i++)
{
printf(&quot;\n\tName : &quot;);
scanf(&quot;%s&quot;, &ptr->name);
printf(&quot;\n\tName : &quot;);
scanf(&quot;%d&quot;, &ptr->identity);
}

}


void print_details(GOLFER player[])
{
int i;
for(i=0;i<=10;i++)
printf(&quot;\n\tNAME\t\t\tID\n&quot;);
printf(&quot;%s %d&quot;,player.name,player.identity);

}
 
Hi qwertyuiop124123,
As per my opinion, after going through your code, you defined the array size of 10. So up to index 9. Remember the array index starts from 0. So memory location player[MAX] will be out of array. But C compiler will not check the bound and will happily allow you to go ahead. First try to refer to the structure variable in the array with in bound limit as for example player[9] i.e last array element.
Secondly, you are using an arrow operator for accessing the data members. That is fine but use of & operator will give you problem. Use pointer in conjunction with arrow operator OR dot operator with variable.
In a function get_details(), Pointer will have to be increamented if you want to get details for all the elements of array. Only increamenting variable i without pointer increament will give details of same elemnts 10 times.

I think this may help you...
Sanjay
 
Thanks for the reply, i`ve made the necessary changes but it still doesn`t compile. Its says

:-error LNK2001: unresolved external symbol &quot;void __cdecl print_details(struct GOLFER)&quot; (?print_details@@YAXUGOLFER@@@Z)
Debug/Jubilee Cup.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

i`ve no idea:((
Code:
#include <stdio.h>
#include <string.h>
#define MAX 10

typedef struct {
				char name[20];
				int identity;
				}GOLFER;

void initial_screen(void);
void get_details(GOLFER *ptr);
void print_details(GOLFER player);

void main (void)
{
	GOLFER player[MAX];
	initial_screen();
	get_details(&player[MAX]);
	print_details(player[MAX]);

}


void initial_screen(void)
{
	printf(&quot;\t*************************************************\n&quot;);
(&quot;\n\t*************************************************\n&quot;);
	puts(&quot;\n\t&quot;);
	puts(&quot;\tInput Players or (x) to finish input&quot;);
}


void  get_details(GOLFER *ptr)
{

	int i;
	for(i=0;i<=9;i++,*(ptr+i))
	{
	printf(&quot;\n\tName	:	&quot;);
	scanf(&quot;%s&quot;, (*ptr).name);
	printf(&quot;\n\tName	:	&quot;);
	scanf(&quot;%d&quot;, (*ptr).identity);
	}

}


void print_details(GOLFER player[])
{
	int i;
	for(i=0;i<=9;i++)
	printf(&quot;\n\tNAME\t\t\tID\n&quot;); 
	printf(&quot;%s      %d&quot;,player[i].name,player[i].identity);

}
 
Here are the problems I found:

1. main() returns int, not void.

2. The prototype for print_details() and the function definition don't match.

3. Your scanf() call in get_details() should be:

scanf(&quot;%s&quot;,ptr->name);
scanf(&quot;%d&quot;, &ptr->identity);

4. In initial_screen() you have a statement with no effect:

(&quot;\n\t*************************************************\n&quot;);

5. In main() you pass print_details() an incompatible type. Change it to this:

print_details(player);

6. You have another no-op in the update part of your for loop in get_details():

for(i=0;i<=9;i++,*(ptr+i))

*(ptr+i) doesn't do anything.

7. Your for loop in print_details() needs braces so that both statements are included in the loop.

After fixing these errors, I got it to compile cleanly. However, it still doesn't do what it says it's apparently supposed to do.

Below is a minimally working version of your code. I say minimally working because there are still problems in the code in that it doesn't handle bad input whatsoever. In fact, the scanf() call with no width restriction is begging for a buffer overflow.

However, from the error messages you're getting from your compiler (you should be getting other messages besides the link error), it sounds like you may have an compiler installation issue. Anyway, try this:

#include <stdio.h>
#include <string.h>
#define MAX 10
#define NAME_MAX 20

typedef struct {
char name[NAME_MAX+1];
int identity;
}GOLFER;

void initial_screen(void);
void get_details(GOLFER *ptr);
void print_details(GOLFER *player);

int main (void)
{
GOLFER player[MAX];
initial_screen();
get_details(player);
print_details(player);
return 0;
}


void initial_screen(void)
{
printf(&quot;\t*************************************************\n&quot;);
puts(&quot;\n\t&quot;);
puts(&quot;\tInput 10 players&quot;);
}


void get_details(GOLFER *ptr)
{
int i;

for(i=0;i<MAX;i++)
{
printf(&quot;Enter name for player %d (max %d characters): &quot;,i,NAME_MAX);
scanf(&quot;%s&quot;,ptr.name);
printf(&quot;Enter identity for player %d (must be a number): &quot;,i);
scanf(&quot;%d&quot;, &ptr.identity);
}
}

void print_details(GOLFER *player)
{
int i;
for(i=0;i<MAX;i++) {
printf(&quot;\n\tNAME\t\t\tID\n&quot;);
printf(&quot;%s %d&quot;,player.name,player.identity);
}
}
Russ
bobbitts@hotmail.com
 
Hi Russ...
As per your suggestion regarding main returning int instead of void, i think main retrurning void also should work. I tried it with VC compiler.

Sanjay
 
It &quot;works&quot; on my compiler as well, though it emits a diagnostic telling me that main() returns int. The only portable definition for main() is either:

int main(void) { /* .. * / }

OR

int main(int argc,char **argv) { /* ... */ }

As a result, any conforming C compiler is allowed to reject any C program that does not provide one of these definitions for main.

Having main() return void only reduces the portability of your code and buys you nothing.
Russ
bobbitts@hotmail.com
 
Thanks for your explaination Russ.I did not think of the portability issue here.

Sanjay
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top