problem accessing a value in a structure

This is a discussion on problem accessing a value in a structure within the C Programming forums, part of the General Programming Boards category; Hi all, I am getting ready to start a project which entails implementing an array adt. To get started and ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    problem accessing a value in a structure

    Hi all,

    I am getting ready to start a project which entails implementing an array adt. To get started and train a bit I have made a structure for testing purposes which is giving me problems.

    More concretely when I compile the whole stuff the compiler tells me that there is an error in the lines that I have commented below. The error says ' incompatible types in assignment'. I have tried removing the scanf's and assigning the values manually like this :
    Code:
           emp1.name = "paul";
           emp1.address = "lexinton ave.";



    but I get the same error. Can anyone give me a hand? I post the code below. Cheers!!


    Code:
    /*
     * test1.c
     *
     *  Created on: Feb 9, 2010
     *      Author
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
    
    	char myname[30];
    	char myaddress[100];
    	int myage;
    	float mysalary;
    
    	struct employee {
    		char name[30];
    		char address[100];
    		int age;
    		float salary;
    	} emp1, emp2;
    
    	printf("Enter the name of the employee: ");
    	scanf("%s", myname);
    	printf("Enter the address of the employee: ");
    	scanf("%s", myaddress);
    	printf("Enter the age of the employee: ");
    	scanf("%d", &myage);
    	printf("Enter the salary of the employee: ");
    	scanf("%f", &mysalary);
    
    	printf("the age of emp1 is: %s\n", emp1.name);
    	printf("the age of emp1 is: %s\n", emp1.address);
    	printf("the age of emp1 is: %d\n", emp1.age);
    	printf("the salary of emp1 is: %f\n", emp1.salary);
    
    	emp1.name = myname; // error: Incompatible types in assignment
    	emp1.address = myaddress; //error: Incompatible types in assignment
    	emp1.age = myage;
    	emp1.salary = mysalary;
    
    	emp1 = emp2;
    
    	return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > emp1.name = myname; // error: Incompatible types in assignment
    Use strcpy() to copy c-style strings.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    Salem is right. You may also want to add
    Code:
    #include <string.h>
    I would also consider moving

    Code:
    printf("the age of emp1 is: %s\n", emp1.name);
    printf("the age of emp1 is: %s\n", emp1.address);
    printf("the age of emp1 is: %d\n", emp1.age);
    printf("the salary of emp1 is: %f\n", emp1.salary);
    after

    Code:
    strcpy(emp1.name, myname);
    strcpy(emp1.address, myaddress);
    emp1.age = myage;
    emp1.salary = mysalary;
    Printing something that has not been given a value is not a good idea.

    Also

    Code:
    emp1 = emp2;
    What is assigned to emp2? Do you mean
    Code:
    emp2 = emp1;

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    problem accessing a value in a structure

    Hi there,

    Thank you for the help!!


    From the suggestion below I understand that the two variables within the structures that are having problems are the ones that have to do with characters.

    Code:
    strcpy(emp1.name, myname);
    strcpy(emp1.address, myaddress);
    emp1.age = myage;
    emp1.salary = mysalary;
    I see that the other two int within the structure have no problem in assigning them the value with scanf.

    I realize see the problem with the order in which I wrote it. The printf's should have been after the value assignement. After the variable declarations, that is.

    However I still don't understand why, If I have declared a string of 30 characters
    I cannot use a scanf to store the value in a variable and then print it with an printf within the context of a structure:


    example 1:

    Code:
    char name [30];
    scanf("%s",name);
    printf("the name is %s",name);

    example 2:

    Code:
    	struct employee { // here we declare the structure
    		char name[30];
    		char address[100];
    		int age;
    		float salary;
    	} emp1, emp2;
    
    	printf("Enter the name of the employee: "); 
    	scanf("%s", myname); 
    	printf("Enter the address of the employee: ");
    	scanf("%s", myaddress);
    	printf("Enter the age of the employee: ");
    	scanf("%d", &myage); // this has no problem
    	printf("Enter the salary of the employee: ");
    	scanf("%f", &mysalary); // this has no problem
    
    	emp1.name = myname; // error: Incompatible types in assignment
    	emp1.address = myaddress; //error: Incompatible types in assignment
    	emp1.age = myage;
    	emp1.salary = mysalary;

    In 'example 2' I declared the structure and within it the chars that will take a value. Then I assigned them with scanf.

    In example 1' there is no structure, I just declared the chars that will take a value.


    Why does the char assignment (with scanf) work in example 1 and it does not work in example 2? I wander if it would work with getchar();??

    Evidently the two int that I declared have no problem only the chars.

    Why??


    Thank you...
    Last edited by buetxxth; 02-11-2010 at 09:46 AM. Reason: typo

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    For the same reason you cannot do this:
    Code:
    char str1[]="hello world", str2[20] = str1;
    If you want to copy the contents of a string, you must use a string function like strcpy() or sprintf().

    You can assign a pointer to a string:
    Code:
    char str1[]="hello world", *ptr = str1;
    I think what you want to do with that struct is just this:
    Code:
    	printf("Enter the name of the employee: "); 
    	scanf("%s", emp1.name);
    Simple enough?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43

    problem accessing a value in a structure

    Hi MK,

    That is exactly what I wanted ;-) The pointer, however, sounds much better. I am going to test it after a few experiments with this example.

    BR,

    Blue

  7. #7
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43
    Hi again,

    there is something seemingly wrong with the compiler I think. Now there is no error at all but it simply gives me an unexpected output.

    I paste the code and the output here

    Code:
    
    /*
     * test1.c
     *
     *  Created on: Feb 9, 2010
     *      Author
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
    
    	struct employee {
    		char name[30];
    		char address[100];
    		int age;
    		float salary;
    	} emp1;
    
    
    	printf("Enter the name of the employee: ");
    	scanf("%s", emp1.name);
    	printf("Enter the address of the employee: ");
    	scanf("%s", emp1.address);
    	printf("Enter the age of the employee: ");
    	scanf("%d", &emp1.age);
    	printf("Enter the salary of the employee: ");
    	scanf("%f", &emp1.salary);
    
    
    	printf("the age of emp1 is: %s\n", emp1.name);
    	printf("the age of emp1 is: %s\n", emp1.address);
    	printf("the age of emp1 is: %d\n", emp1.age);
    	printf("the salary of emp1 is: %f\n", emp1.salary);
    
    
    	return 0;
    }

    Here is the output:

    Enter the name of the employee: Enter the address of the employee: Enter the age of the employee: Enter the salary of the employee: the age of emp1 is: 
    the age of emp1 is: @
    the age of emp1 is: 280
    the salary of emp1 is: 0.000000

    It does not return what is expected plus I see that there is a message when I compile it that says


    **** Build of configuration Debug for project test1 ****

    **** Internal Builder is used for build ****
    Nothing to build for test1

    I use eclipse cpp.


    Can anyone help?

    br,

    blue

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Accessing Variables
    By vileoxidation in forum C++ Programming
    Replies: 10
    Last Post: 10-05-2009, 07:58 AM
  2. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  3. Replies: 9
    Last Post: 05-21-2007, 12:10 AM
  4. accessing structure pointer problem
    By godhand in forum C Programming
    Replies: 2
    Last Post: 04-09-2004, 10:52 PM
  5. Pointer to structure problem
    By unregistered in forum C Programming
    Replies: 3
    Last Post: 12-24-2001, 06:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21