Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
#include <stdio.h>
#include <string.h>

typedef struct
{
     int id;
     char name[30];
     float percentage;
}status, *var1;

int main()
{

status data;
var1=&data;
strcpy(var1->name,"HelloWorld");
var1->id=100;
var1->percentage=200;
printf("\nEmployee Name : %s\n", var1->name);
printf("\nEmployee ID : %d\n", var1->id);
printf("\nEmployee percentage : %f\n", var1->percentage);
return 0;
}


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 16-Jun-15 4:25am
v3
Comments
Mathew Soji 16-Jun-15 10:27am    
What is the compilation error you are getting?
Member 11372005 16-Jun-15 11:16am    
i'm getting below error while trying to complie:

gcc -o struct struct.c
struct.c: In function ‘main’:
struct.c:15:5: error: expected identifier or ‘(’ before ‘=’ token
var1=&data;
^
struct.c:16:8: error: expected expression before ‘var1’
strcpy(var1->name,"HelloWorld");
^
struct.c:16:8: error: too few arguments to function ‘strcpy’
struct.c:17:5: error: expected identifier or ‘(’ before ‘->’ token
var1->id=100;
^
struct.c:18:5: error: expected identifier or ‘(’ before ‘->’ token
var1->percentage=200;
^
struct.c:20:34: error: expected expression before ‘var1’
printf("\nEmployee Name : %s\n", var1->name);
^
struct.c:21:32: error: expected expression before ‘var1’
printf("\nEmployee ID : %d\n", var1->id);
^
struct.c:22:40: error: expected expression before ‘var1’
printf("\nEmployee percentage : %f\n", var1->percentage);
^
Richard MacCutchan 16-Jun-15 12:42pm    
Why are you using var1, when you already created the structure with the name data? Just remove the line var1=&data;, and replace all var1-> with data. .
OriginalGriff 16-Jun-15 13:36pm    
I'm guessing homework and he's supposed to use a pointer.
Member 11372005 17-Jun-15 9:44am    
yes i want to assign values to the members of the structure using arrow operator, not using a dot operator.

1 solution

It helps if you tell us the error message! But...
Change this:
C++
typedef struct
{
     int id;
     char name[30];
     float percentage;
}status, *var1;

To this:
C++
typedef struct
{
     int id;
     char name[30];
     float percentage;
}status;
status *var1;
 
Share this answer
 
Comments
Member 11372005 16-Jun-15 11:13am    
I'm getting below error while trying to compile

gcc -o struct struct.c
struct.c: In function ‘main’:
struct.c:15:5: error: expected identifier or ‘(’ before ‘=’ token
var1=&data;
^
struct.c:16:8: error: expected expression before ‘var1’
strcpy(var1->name,"kodiak");
^
struct.c:16:8: error: too few arguments to function ‘strcpy’
struct.c:17:5: error: expected identifier or ‘(’ before ‘->’ token
var1->id=100;
^
struct.c:18:5: error: expected identifier or ‘(’ before ‘->’ token
var1->percentage=200;
^
struct.c:20:34: error: expected expression before ‘var1’
printf("\nEmployee Name : %s\n", var1->name);
^
struct.c:21:32: error: expected expression before ‘var1’
printf("\nEmployee ID : %d\n", var1->id);
^
struct.c:22:40: error: expected expression before ‘var1’
printf("\nEmployee percentage : %f\n", var1->percentage);
OriginalGriff 16-Jun-15 11:26am    
And if you change it as above?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900