|
-
February 9th, 2009, 01:30 AM
#1
Structure Array
Hi...........
I wrote the array in struct in C like....
Code:
struct Employee
{
char EmployeeName[40];
char EmployeeDesignation[40];
char EmployeeTechnology[40];
int EmployeeSalCode;
}EmployeeArray[7], *Employeeptr;
When I feed information to it ....I get error....
Code:
EmployeeArray[1].EmployeeName = "Yogesh Zambre";
EmployeeArray[1].EmployeeDesignation = "Software Engineer";
EmployeeArray[1].EmployeeTechnology = "Assembly, C, C++";
EmployeeArray[1].EmployeeSalCode = 12588;
Error: error C2106: '=' : left operand must be l-value
I searched on the google to remove this error, but didnot get much.
Please tell me how to overcome this problem.
I have been suggested not to use Pointer inside the Structure.
-
February 9th, 2009, 02:02 AM
#2
Re: Structure Array
You are trying to assign to arrays. Assuming that you do not actually want to initialise the array of structures (i.e., when defining it), you should use strcpy() to copy over each string literal's contents to the respective member.
-
February 9th, 2009, 02:21 AM
#3
Re: Structure Array
In addition you have to make sure you're not copying strings with more 40 characters, including the null terminator.
-
February 9th, 2009, 03:12 AM
#4
Re: Structure Array
 Originally Posted by laserlight
You are trying to assign to arrays. Assuming that you do not actually want to initialise the array of structures (i.e., when defining it), you should use strcpy() to copy over each string literal's contents to the respective member.
Hi....
Thank you for the help!!!
I added the strcpy() function, it is solved, but it is getting new problem now.....
Code:
strcpy(EmployeeArray[5].EmployeeName,"Yogesh Zambre");
strcpy(EmployeeArray[5].EmployeeDesignation,"Software Engineer");
strcpy(EmployeeArray[5].EmployeeTechnology,"Assembly, C, C++");
EmployeeArray[5].EmployeeSalCode = 12588;
The strcmp function is giving me warning....
Code:
else if( SUCCESS == ( strcmp( Name, EmployeeArray[5].EmployeeName) ) )
{
printf( "Name:\t\t%s\n", EmployeeArray[5].EmployeeName);
printf( "Designation:\t%s\n ", EmployeeArray[5].EmployeeDesignation);
printf( "Technology:\t%s\n ", EmployeeArray[5].EmployeeTechnology);
printf( "Salcode:\t%d\n ", EmployeeArray[5].EmployeeSalCode);
}
Warning: warning C4024: 'strcmp' : different types for formal and actual parameter 1
warning C4047: 'function' : 'const char *' differs in levels of indirection from 'ch
The strcpy is also giving one warning
Warning: 'strcpy' was declared deprecated
-
February 9th, 2009, 03:49 AM
#5
Re: Structure Array
If you were to use std::string then your original code would be fine
Code:
#include <string>
struct Employee
{
std::string EmployeeName;
std::string EmployeeDesignation;
std::string EmployeeTechnology;
int EmployeeSalCode;
}EmployeeArray[7], *Employeeptr;
Also the strings are now not limited to 39 characters. (and don't produce a nasty bug when they do exceed it)
If you need a variable number of employees then use std::vector
Code:
#include <vector>
std::vector<Employee> EmployeeArray;
See http://www.cppreference.com/wiki/
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
February 9th, 2009, 04:41 AM
#6
Re: Structure Array
I could resolve the issue....
I added this.....
#define _CRT_SECURE_NO_DEPRECATE 1
Anyways..thank you for the help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|