Structure Array
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Structure Array

  1. #1
    Join Date
    Dec 2008
    Posts
    86

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,315

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Structure Array

    In addition you have to make sure you're not copying strings with more 40 characters, including the null terminator.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Dec 2008
    Posts
    86

    Re: Structure Array

    Quote Originally Posted by laserlight View Post
    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

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,725

    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

  6. #6
    Join Date
    Dec 2008
    Posts
    86

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center