member selection operator program
Why this program is not working??
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
#include<cstring>
using namespace std;
struct Employee{
char first_name[16];
};
int main()
{ struct Employee *p_emp;
struct Employee emp;
strcpy(p_emp->first_name,"Kumar");
cout<<emp.first_name;
return 0;
}
|
Compiler says:
In function 'int main()':
12:37: warning: 'p_emp' is used uninitialized in this function [-Wuninitialized] |
The p_emp is a pointer. Uninitialized pointer.
The strcpy writes characters to a member of an Employee object.
Which object does the p_emp point to? Where does an uninitialized pointer point to?
Nobody knows.
I did make some changes, and it worked.
Thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
using namespace std;
struct Employee{
char first_name[16];
};
int main()
{ struct Employee *p_emp;
struct Employee emp;
p_emp=&emp;
strcpy(p_emp->first_name,"Kumar");
cout<<emp.first_name;
return 0;
}
|
Topic archived. No new replies allowed.