![]() |
| | #1 |
| Registered User Join Date: Feb 2010
Posts: 2
| problem accessing a value in a structure 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 | |
| | #2 |
| and the hat of Destiny 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. |
| Salem is offline | |
| | #3 |
| Registered User Join Date: Apr 2006
Posts: 48
| Salem is right. You may also want to add Code: #include <string.h> 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);
Code: strcpy(emp1.name, myname); strcpy(emp1.address, myaddress); emp1.age = myage; emp1.salary = mysalary; Also Code: emp1 = emp2; Code: emp2 = emp1; |
| samf is offline | |
| | #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 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 | |
| | #5 |
| dat is, vast staat 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; You can assign a pointer to a string: Code: char str1[]="hello world", *ptr = str1; Code: printf("Enter the name of the employee: ");
scanf("%s", emp1.name);
__________________ 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 | |
| | #6 |
| Registered User 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 | |
| | #7 |
| Registered User 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |