Initialising the structure

This is a discussion on Initialising the structure within the C Programming forums, part of the General Programming Boards category; Code: struct X { char a[10]; char b[10]; }; struct X x; memset(&x,0x20,sizeof(x)); will it initialize all the variable in ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Location
    Hyderabad
    Posts
    33

    Initialising the structure

    Code:
    struct X
    {
          char a[10];
          char b[10];
    };
    
    struct X x;
    memset(&x,0x20,sizeof(x));
    will it initialize all the variable in structure to spaces?

  2. #2
    Registered User
    Join Date
    Jul 2007
    Location
    Hyderabad
    Posts
    33
    Code:
    struct X
    {
          char a[10];
          char b[10];
    };
    
    struct X x;
    memset(&x,0x20,sizeof(struct X));
    will it initialize all the variable in structure to spaces?

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yes, but it will not add any '\0' so a & b are still invalid strings.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Jul 2007
    Location
    Hyderabad
    Posts
    33
    Quote Originally Posted by MK27 View Post
    Yes, but it will not add any '\0' so a & b are still invalid strings.
    Yes that's i got the same too from below output after running the code,
    a1:### ��###
    b1:### ��###
    c1:### ��###

    But is there any possibility to acheive this.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Not with a single memset, no. Not any easier than just either doing it in the first place by hand, or by calling strncpy or something.
    Code:
    struct foo bar = { "...9 spaces...", "...9 spaces..." };

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help me as fast as possible
    By Xbox999 in forum C Programming
    Replies: 5
    Last Post: 11-30-2009, 06:53 PM
  2. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 06:46 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM

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