Struct

This is a discussion on Struct within the C Programming forums, part of the General Programming Boards category; Code: struct car { char name[40]; }car1; how would i be able to initialize that var to mustang for ex.?...

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    Struct

    Code:
    struct car {
     char name[40];
    }car1;
    how would i be able to initialize that var to mustang for ex.?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    strncpy( car1.name, "Mustang", 39);
    car1.name[30]=NULL;

    There are many ways to do it. That is a pretty good one.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    how come i cant do
    car.name = "Mustang";

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by MethodMan
    how come i cant do
    car.name = "Mustang";
    You can only do string assignments that way during creation of the variable.

    struct car car1 = { "Mustang" };

    The other reason you can't is that this is C. In C, you cannot just assign strings values to arrays like that. In C++ it's possible to override the assignment operator, and you could then do that, but not in C. Strings are not handled the same as integers or what not. The reason they aren't is that there is no "string" type in C. "Strings" are just arrays of characters.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Unregistered
    Guest
    For the same reason you can't do

    char name[40] = "firstline";

    name = "anotherline";

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    You could do

    car1.name[22] = 'A';

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Barjor
    You could do

    car1.name[22] = 'A';
    Right, because here you're assigning a single character, not an array of characters in one shot. (For the original poster's sake.)

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    do you mean you want to assign it on creation or create a variable of that data type and then assign it, if so you would do something like:
    Code:
    struct car {
     char name[40];
    }car1;
    
    int main()
    {
      car1 tempcar;
    
      strcpy(tempcar.name,"Mustang");
    }
    not 100% which way ya meant, if its assign it as a constant for all the ones you create then the previous posts are the way to go.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Bull
    do you mean you want to assign it on creation or create a variable of that data type and then assign it, if so you would do something like:
    Code:
    struct car {
     char name[40];
    }car1;
    
    int main()
    {
      car1 tempcar;
    
      strcpy(tempcar.name,"Mustang");
    }
    not 100% which way ya meant, if its assign it as a constant for all the ones you create then the previous posts are the way to go.
    Your code won't compile for two reasons:
    a) 'car1 tempcar' is not valid. 'car1' is an instance of 'struct car'. It is global so you do not need 'tempcar'.
    b) 'car1' is not a typedef, nor is it the name of the structure.

    I prefer strncpy, as shown above. Otherwise...

    strcpy( car1.name, "this really long car name just killed your application" );

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM

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