Thread: struct data input problem..

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by transgalactic2 View Post
    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

    ??
    This is the same as doing
    Code:
    int two_d_array[2][4] = {{1,2,3,4},{5,6,7,8}};
    I have to do the whole array right here, before the semicolon, or I lose my chance. Here each struct would be initialized with a bunch of things in curly braces; to initialize the array I take all of them and surround them in an extra pair of curly braces.

  2. #17
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    in this example
    we do 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:
    #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
    Last edited by transgalactic2; 10-25-2008 at 01:33 PM.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is not possible.
    And for goodness sake, change that struct of yours!
    Code:
    struct robot { 
      const char* name; 
      int energy; 
    };
    Error 1 error C2086: 'ROBOT robots[3]' : redefinition g:\w00t\visual studio 2008\projects\temp\temp3.cpp 13
    Last edited by Elysia; 10-25-2008 at 01:28 PM.
    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.

  4. #19
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i took this example from this tutorial:
    http://irc.essex.ac.uk/www.iota-six...._to_struct.asp

    its the last topic on the page
    called

    "Arrays of Structure Variables"

    where is my mistake in understanding this example?

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    They do not redefine the variable. It is enclosed in comments.
    And despite comments, the code inside the comments actually compiled file (remember to remove the first defining of the variable).
    But the example does have one thing wrong, however.
    If you're going to assign string literals to variables, then use const char*, not char*.

    Oh and I found this:
    Instinctively, I first declared my array of robots, then I tried initializing each element one by one like this:
    robots[0] = {"Lunar Lee", 50};
    Unfortunately, MSVC++ complained, and so I initialized each name and energy field separately for each robot, as shown above.
    I just want to point out that this is wrong, WRONG, WRONG!
    MSVC is right to complain here, because it is not possible according to the standard.
    Better find yourself a better tutorial... Try this site's tutorials, instead.
    Last edited by Elysia; 10-25-2008 at 01:42 PM.
    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.

  6. #21
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks

    so curly braces can go ones only at the initialization of the variable

    why do i need to change the char pointer into constant?
    Last edited by transgalactic2; 10-25-2008 at 01:51 PM.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by transgalactic2 View Post
    so curly braces can go ones only at the initialization of the variable
    Only at the definition or declaration of a variable, as pointed out.

    why do you want to change the char pointer into constant?
    I already mentioned that. Try following links.
    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.

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by transgalactic2 View Post

    why do you want to change the char pointer into constant?
    It should be a pointer to constant characters if you intend to always use string literals for the names, since string literals are constant characters (as opposed to just plain characters).

  9. #24
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    if i get it correctly
    string literal is a char pointer to which we initialized it using curly braces

    string literals are on read only memory

    and there are no way to change them after initialization
    Last edited by transgalactic2; 10-25-2008 at 02:15 PM.

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, a string literals is something like this:
    "Hello World!"
    String literals are read-only, yes.
    They cannot ever be changed, since they are written correctly from the start (know what I mean?).
    A string literals is typically stored in a read-only place and the compiler then assigns a const char* pointer to wherever you assign or initialize it.
    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. #26
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so its not only about char pointers
    Code:
    char str[]="hello world";
    str also a string literal

    do i need to keep it as a constant too?

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    When initializing an array with a string literal, the string literal is copied into the buffer, so you don't need to make it constant.
    Code:
    char* str = "Hellq World!";
    str[4] = 'o'; // Oops. Error at runtime.
    char str2[] = "Hellq World!";
    str2[4] = 'o'; // OK
    const char* str3 = "Hellq World!";
    str3[4] = 'o'; // Oops. Compile error.
    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.

  13. #28
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ok so this constant read only memory thing only goes
    for char pointers string literals?

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    When assigning to (or initializing) pointers, yes, then the pointer should be const (pointer to const char).
    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.

  15. #30
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks

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