define string to struct var

This is a discussion on define string to struct var within the C Programming forums, part of the General Programming Boards category; Code: can we define string to structure variable without using strcpy? For ex: my_struct.lname="Jensen"; instead of strcpy(my_struct.lname,"Jensen");...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    32

    Lightbulb define string to struct var

    Code:
    can we define string to structure variable without using strcpy?
    
    For ex:
    
    my_struct.lname="Jensen";
    instead of strcpy(my_struct.lname,"Jensen");

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Nope... you need to use strcpy()

    C doesn't know strings. Only it's library functions do.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    32

    Lightbulb problem in string assignment

    Code:
    But we can assign string to normal variables 
    
    for ex:
    
              char *str="live";
              char str[5]="live";
    
    How is it? If u know please reply.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Why are you putting everything in code tag?
    char str[5]="live"; // this is array initialization.
    Question 6.6b

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by siperi View Post
    Code:
    But we can assign string to normal variables 
    
    for ex:
    
              char *str="live";
              char str[5]="live";
    
    How is it? If u know please reply.
    The C compiler allows "initializers"... when you do what you are showing you are intializing a new variable to it's first value. You will still need strcpy() to change that value later.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detailed ASCII Pong Game, please review!
    By parad0x13 in forum Game Programming
    Replies: 5
    Last Post: 08-02-2010, 10:45 PM
  2. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  3. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM

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