Whats the meaning of this code

Discussion in 'C++' started by sam, Apr 23, 2007.

  1. sam

    sam Guest

    HI,
    Whats the meaning of this code:-

    char buff[100];
    memset(buff, 'A' , 100);
    sometimes we can place hex value in
    place of 'A' like 0x90c
    but how this code function actually.

    thanks in advance
     
    sam, Apr 23, 2007
    #1
    1. Advertising

  2. sam wrote:
    > HI,
    > Whats the meaning of this code:-
    >
    > char buff[100];
    > memset(buff, 'A' , 100);
    > sometimes we can place hex value in
    > place of 'A' like 0x90c


    I doubt that 0x90c can replace 'A' generally. The value 0x90c
    is larger than the value a 'char' object can contain (usually).

    > but how this code function actually.


    The declaration/definion of 'buff' functions by declaring and
    defining that array. It's left uninitialised. How exactly it
    is allocated is unspecified. Elements of 'buff' have automatic
    storage duration, most likely (since those two lines of code
    are undoubtedly part of a function body).

    'memset' is a function, so the second statement calls the
    function and passes it three arguments. The description of
    'memset' says that it fills the memory pointed to by the first
    argument with the value of the second argument, counting the
    bytes using the third argument. IOW, the entire array 'buff'
    gets filled with characters 'A', all 100 elemenst of 'buff'
    will have the value 'A' after 'memset' returns.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 23, 2007
    #2
    1. Advertising

  3. sam

    Guest

    On Apr 23, 9:05 am, sam <> wrote:
    > HI,
    > Whats the meaning of this code:-
    >
    > char buff[100];
    > memset(buff, 'A' , 100);
    > sometimes we can place hex value in
    > place of 'A' like 0x90c
    > but how this code function actually.
    >
    > thanks in advance

    This is just setting a memory area (buff) with
    initial values.
     
    , Apr 23, 2007
    #3
  4. wrote:
    > On Apr 23, 9:05 am, sam <> wrote:
    >> HI,
    >> Whats the meaning of this code:-
    >>
    >> char buff[100];
    >> memset(buff, 'A' , 100);
    >> sometimes we can place hex value in
    >> place of 'A' like 0x90c
    >> but how this code function actually.
    >>
    >> thanks in advance

    > This is just setting a memory area (buff) with
    > initial values.


    I would omit the word "initial" from this explanation.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 23, 2007
    #4
  5. sam

    sam Guest

    On Apr 23, 6:05 pm, sam <> wrote:
    > HI,
    > Whats the meaning of this code:-
    >
    > char buff[100];
    > memset(buff, 'A' , 100);
    > sometimes we can place hex value in
    > place of 'A' like 0x90c
    > but how this code function actually.
    >
    > thanks in advance


    I am not saying 'A' is replaced by
    0x90c but i am saying we can
    place any hex value as per
    requirement in place of 'A'
    and I want brief explanation.
     
    sam, Apr 23, 2007
    #5
  6. sam wrote:
    > On Apr 23, 6:05 pm, sam <> wrote:
    >> HI,
    >> Whats the meaning of this code:-
    >>
    >> char buff[100];
    >> memset(buff, 'A' , 100);
    >> sometimes we can place hex value in
    >> place of 'A' like 0x90c
    >> but how this code function actually.
    >>
    >> thanks in advance

    >
    > I am not saying 'A' is replaced by
    > 0x90c but i am saying we can
    > place any hex value as per
    > requirement in place of 'A'
    > and I want brief explanation.


    An explanation would probably be that 'char' is a type that
    can store values (of a certain range). 'A' represents a value
    that can be stored in a 'char' object. So does 0 and any
    other integral that can be converted to 'char'. The hex
    notation is just a way of representing the value. For example,
    in ASCII, 0x41 is the hex equivalent of 'A' (the capital form
    of the first letter of the Latin alphabet). 65 is the decimal
    equivalent of 0x41 (and of 'A'). 0101 is the octal equivalent.

    RTFM on memset. Then build a minimal program with your code
    and run it under a debugger, looking at the elements of the
    array 'buff' as you step through your code.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 23, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Noah Roberts

    Re: Whats the meaning of this code

    Noah Roberts, Jan 24, 2007, in forum: C Programming
    Replies:
    5
    Views:
    407
    santosh
    Jan 24, 2007
  2. sam
    Replies:
    3
    Views:
    310
    Jim Langston
    Jan 24, 2007
  3. sam
    Replies:
    3
    Views:
    280
    Ron Natalie
    Jan 30, 2007
  4. sam
    Replies:
    4
    Views:
    354
    gtkmm
    Sep 6, 2008
  5. Replies:
    4
    Views:
    787
Loading...

Share This Page