Thread: struct data input problem..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    struct data input problem..

    when i got a normal struct variable code:
    Code:
    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:
    #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
    Last edited by transgalactic2; 10-24-2008 at 03:33 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    But this does:
    Code:
    ROBOT robots[3] = {{"Lunar Lee", 50}, {"Terran Tim", 75}, {"Martian Marsha", 30}};
    You can only initialize a variable when it is declared, never afterward.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    but i did declared it first

    ROBOT robots[3];

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Once you hit that semicolon, your chance at initializing the variable is over -- do it then, or forever hold your peace. (After that, you'll have to assign things normally without benefit of curly braces.)

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    when i do
    ROBOT robots[3];

    every cell of robots without a value
    you said that after this i can only assign values like:
    robots[0].energy=5
    etc..

    why i cant using curly braces
    whats so special about it?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's initialization, simply put.
    After you've defined something, you cannot initialize it. You can only assign it.
    That's the way it works.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    initialization is when i create a new variable of some data type
    so is define

    what is the difference between two of those terms?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, it is not.
    Definition is creating something, in simple words.
    Initialization is when you assign a value when creating it.
    Assigning is when you assign something after it has been created.

    Code:
    int X = 5; // Definition of variable, initializing it with value 5.
    int x2; // Defining variable x2
    x2 = 5; // Assigning 5 to x2
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Quote Originally Posted by Elysia View Post
    Initialization is when you assign a value when creating it.
    Assigning is when you assign something after it has been created.
    Using the term 'assign' in the definition while trying to differentiate the terms 'assign' and 'initialize' seems like a really bad idea.

    How about this:

    Initialize: To specify the very first value that an object will have after it is created.
    Assign: To set the value of the object on the left-hand-side equal to the value of the object on the right-hand-side. The left-hand-side must evaluate to a modifiable lvalue (meaning you can't write "5 = x + 1;" since 5 isn't a modifiable lvalue). The right-hand-side must evaluate to a legal rvalue.

    Furthermore, to help clarify things, think of any value or list of values in curly braces as an "initializer list".
    Code:
    int A[5];
    
    A = {1, 2, 3, 4, 5};  // Illegal, you can't assign an initializer list to an object
                          // Initializer lists can only be used to initialize objects
    Maybe this will help you sort it out in your head. It helps me at least.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by arpsmack View Post
    Using the term 'assign' in the definition while trying to differentiate the terms 'assign' and 'initialize' seems like a really bad idea.
    I don't know, but your explanation seems more detailed and better, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ok so in my first example
    i am doing initialization

    in the second example
    first i am defining a variable robots[3] (all of it cells are without data)

    then i am assigning values to one of it cells
    Code:
    why it ok to  do
    robot[0].name="Lunar Lee";
    robots[0].energy=5;
    
    but not
    robots[0] = {"Lunar Lee", 50};
    they both changing the values of cell 0
    ??

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ahhhh curly brACES CAN BE USED ONLY ones at the declaration of the variable


    so if it true then
    why we can do this after we initialize robots[3]
    Code:
    ROBOT robots[3] = {{"Lunar Lee", 50}, {"Terran Tim", 75}, {"Martian Marsha", 30}};
    this line comes against this rule in which we cant initialize more then ones

    ??

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by transgalactic2 View Post
    ok so in my first example
    i am doing initialization

    in the second example
    first i am defining a variable robots[3] (all of it cells are without data)

    then i am assigning values to one of it cells
    Code:
    why it ok to  do
    robot[0].name="Lunar Lee";
    robots[0].energy=5;
    
    but not
    robots[0] = {"Lunar Lee", 50};
    they both changing the values of cell 0
    ??
    First is assigning, second is initialization (which can be done only at the point when you create the variable).
    And the second post works because you are creating a new robot array and initializing it.
    Also, string literals should be const: http://cpwiki.sourceforge.net/Common...kes_and_errors
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so when i do

    Code:
    ROBOT robots[3] ;
    then
    
    ROBOT robots[3] = {{"Lunar Lee", 50}, {"Terran Tim", 75}, {"Martian Marsha", 30}};
    the second line replaces the old robots[3] variables
    and builds a new robots[3] ??

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Nope, it will give a compile error (robots already defined).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Problem with Printing message
    By robert_sun in forum C Programming
    Replies: 2
    Last Post: 05-16-2004, 02:09 PM