Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Struct data input problem

  1. Oct 24, 2008 #1
    when i got a normal struct variable code:
    Code (Text):

    struct human {
      char name[50];
      int age;
      float height;
    };

    struct human TR = {"Tony", 12, 1.5};   \\this works
     
    but when my object is a part of an array and i take just one object
    the same data input method doesnt work
    why is that?
    Code (Text):

    #include <stdio.h>

    typedef struct robot ROBOT;

    struct robot {
      char *name;
      int energy;
    };

    int main() {
      int i;
     
      ROBOT robots[3];

     
       robots[0] = {"Lunar Lee", 50};  //this  dont work
     
     
  2. jcsd
  3. Oct 25, 2008 #2

    jtbell

    User Avatar

    Staff: Mentor

    The first is initialization of the struct. The specified data is placed in the struct when the memory is allocated before the program begins execution. The second is an attempt to assign values to the struct during program execution, after memory has been allocated. C and C++ allow you to use that syntax only in initialization. Why, I don't know. Maybe the language designers were simply lazy. :smile:

    Note that you have the same situation with arrays.
     
  4. Oct 25, 2008 #3

    uart

    User Avatar
    Science Advisor

    Is there also a second problem with the last piece of code in that name is never allocated any memory, or is the allocation made automatically when a constant string like "lunar lee" is defined.

    BTW I'm just checking because I'm primarily a Pascal programmer and only half fluent in C.



    Actually the more I think about it the more I seem to remember that constant strings like that are automatically assigned memory. But say he used name as a target for strcpy in the second bit of code without assigning memory to it. Then he'd be in trouble right?
     
    Last edited: Oct 25, 2008
  5. Oct 25, 2008 #4

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    That's right. Consider this code:
    Code (Text):

    const char * name;
    double radius, diam;
    ...
    name = "Circle";
    diam = 3.14159265358979323846 * radius;
    ...
     
    The compiled code will contain data sections that include the string "Circle" and the number 3.14159265358979323846 (in the machine's native form for doubles, of course).
    That is also correct. Suppose a chunk of code defines two variables char *string1, *string2;. The statements strcpy (string1, "Tony"); and string2="Tony"; do very different things. The first statement does not change the value of string1; it instead changes what string1 points to. If string1 doesn't point to a valid chunk of memory the string copy will fail. The second statement is just an assignment statement. Just like diam = 3.14159265358979323846 * radius; changes the value diam, the statement string2="Tony"; changes the value of string2.
     
  6. Oct 25, 2008 #5
    regarding to jtbell:
    when you say " The first is initialization of the struct."

    i am building a new type called human.

    "The specified data is placed in the struct when the memory is allocated before the program begins execution. "

    the data is not placed in the struct
    its placed in the variable TR

    "The second is an attempt to assign values to the struct during program execution, after memory has been allocated. "

    i cant see the problem
    first we create an array of some data type we created
    by doing that we have a memory space for this array
    then we take one cell from this array(it consists of 2 variables) and put there our values

    of course it should happen during the execution of the program

    what the memory problem?
     
  7. Oct 25, 2008 #6
    in this example
    we create the same variable twice
    but here on the second time we are creating robots[3] using ROBOT (which is the same type as robot type)

    why is it possible??

    we are creating the same variable twice
    Code (Text):

    #include <stdio.h>

    typedef struct robot ROBOT;

    struct robot {
      char *name;
      int energy;
    };

    int main() {
      int i;
     
      ROBOT robots[3];                              //1st time

      robots[0].name = "Lunar Lee";
      robots[0].energy = 50;
      robots[1].name = "Planetary Pete";
      robots[1].energy = 20;
      robots[2].name = "Martian Matt";
      robots[2].energy = 30;

     
      ROBOT robots[3] = { {"Lunar Lee", 50},{"Planetary Pete", 20},{"Martian Matt", 30} }; //2nd time
     
     
  8. Oct 25, 2008 #7
    thanks i got the idea
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Struct data input problem
Loading...