Structure problem

This is a discussion on Structure problem within the C Programming forums, part of the General Programming Boards category; Hi, i'm having a problem with the following structure I created. I'm getting an error message that rd.name is not ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    9

    Question Structure problem

    Hi, i'm having a problem with the following structure I created. I'm getting an error message that rd.name is not an l-value. I don't understand why the compiler is not recognizing it as an l-value. Here's the code:

    Code:
    #include <stdio.h>
    
    int main()
    {
    	struct record
    	{
    		char name[12];
    		short age;
    		int salary;
    	};
    	
    	struct record rd;
    
    
    	rd.name = "James Freda";
    	rd.age = 24;
    	rd.salary = 20000;
    	
    	printf("My name is %s, I am %h years old, and \
    my salary is %d.  Not much huh?\n\n", rd.name,rd.age,rd.salary);
    
    	return 0;
    }
    Thanks in advance for help anyone has to offer.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    161
    An array cannot be used as an lvalue. Try using strcpy():

    Code:
    #include <stdio.h>
    
    int main()
    {
    	struct record
    	{
    		char name[12];
    		short age;
    		int salary;
    	};
    	
    	struct record rd;
    
    
    	strcpy(rd.name, "James Freda");
    	rd.age = 24;
    	rd.salary = 20000;
    	
    	printf("My name is %s, I am %h years old, and \
    my salary is %d.  Not much huh?\n\n", rd.name,rd.age,rd.salary);
    
    	return 0;
    }

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    The %h is an incorrect format specifier; I think you want %hd.
    Code:
    #include <stdio.h>
    
    struct record
    {
       char name[12];
       short age;
       int salary;
    };
    
    int main(void)
    {
       struct record rd = { "James Freda", 24, 20000 };
    
       printf("My name is %s, I am %hd years old, and "
              "my salary is %d.  Not much huh?\n\n",
              rd.name, rd.age, rd.salary);
    
       return 0;
    }
    
    /* my output
    My name is James Freda, I am 24 years old, and my salary is 20000.  Not much huh?
    */
    I chose to use an initialization for rd -- to show you another way to do the same thing -- instead of leaving it uninitialized and assigning to it later. You can only do initialization at the rd's definition, and you would need to do a strcpy() if you later wanted to change rd.name.

    This code also shows a different way to split a long string.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    9

    Re: Structure problem(Thank you)

    Thanks to both of you for clearing this problem up. I also like the alternate way of defining the variables in the structure. I'm still working my way through these long books like "C Primer Plus", so I find having a great place like this message board to ask questions helps the learning process go faster. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem getting structure to work in my function
    By Tom Bombadil in forum C Programming
    Replies: 18
    Last Post: 05-28-2009, 09:53 AM
  2. Problem with structure and class
    By Bargi in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2007, 02:30 AM
  3. Problem with arrays inside a structure
    By babu in forum C Programming
    Replies: 4
    Last Post: 07-12-2007, 09:35 AM
  4. accessing structure pointer problem
    By godhand in forum C Programming
    Replies: 2
    Last Post: 04-09-2004, 10:52 PM
  5. Problem checking for numeric value in a structure
    By ronkane in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2002, 01:53 PM

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