C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-09-2010, 12:56 PM   #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;
}
buetxxth is offline   Reply With Quote
Old 02-09-2010, 01:25 PM   #2
and the hat of Destiny
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 22,495
> 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.

Salem is offline   Reply With Quote
Old 02-09-2010, 01:34 PM   #3
Registered User
 
Join Date: Apr 2006
Posts: 48
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;
samf is offline   Reply With Quote
Old 02-11-2010, 10:44 AM   #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 10:46 AM. Reason: typo
buetxxth is offline   Reply With Quote
Old 02-11-2010, 10:54 AM   #5
dat is, vast staat
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 6,612
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
GDB tutorial #1 -- gnu debugger tutorials -- GDB tutorial #2
cpwiki -- our wiki on sourceforge
MK27 is offline   Reply With Quote
Old 02-11-2010, 11:24 AM   #6
Registered User
 
bluetxxth's Avatar
 
Join Date: Feb 2010
Location: sweden
Posts: 33
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
bluetxxth is offline   Reply With Quote
Old 02-11-2010, 12:30 PM   #7
Registered User
 
bluetxxth's Avatar
 
Join Date: Feb 2010
Location: sweden
Posts: 33
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
bluetxxth is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Accessing Variables vileoxidation C++ Programming 10 10-05-2009 07:58 AM
Problem referencing structure elements by pointer trillianjedi C Programming 19 06-13-2008 05:46 PM
Problem writing structure within another structure to file Astoria C Programming 9 05-21-2007 12:10 AM
accessing structure pointer problem godhand C Programming 2 04-09-2004 10:52 PM
Pointer to structure problem unregistered C Programming 3 12-24-2001 07:54 AM


All times are GMT -6. The time now is 12:13 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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