Thread: 0x00 in C

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    26

    Lightbulb 0x00 in C

    Hi all,

    Just had q query wrt to the string "0x00" used in memset of character arrays in C. Could anyone reply on this!!

    Thanks,
    Vandrea

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I don't understand your question. You don't pass a string to memset. If you're talking about the integral constant 0x00, it's just the hex encoding of, well, 0.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    26

    Smile

    yep u were right..i meant of the integral constant!

    Consider the follg eg:
    Code:
    char abc_ca[3];
    memset(abc_ca,0x00,size(abc_ca));
    So after the above memset i would have following values in my string

    Code:
    abc[0]={'\0'} 
    abc[1]={'\0'} 
    abc[2]={'\0'}
    where {'\0'} identifies the end of string...Just wanted to know if my understanding is right!

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Yep. On a side note, though, just keep in mind that sizeof used in that way won't work on an array passed to a function or on a pointer to an array.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    26
    Thanks Sebastiani for the quick reply...but dint get as to why cant we use sizeof in memset ?
    Last edited by vandrea; 08-16-2009 at 04:22 AM. Reason: error

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, for example:

    Code:
    int main( void )
    {	
    	char 
    		buf[ 1024 ], 
    		* ptr = buf;
    	printf("sizeof( buf ): %d\n", sizeof( buf ) );
    	printf("sizeof( ptr ): %d\n", sizeof( ptr ) );
    	return 0;
    }
    And when you pass an array to a function, it resolves to a pointer, so the effect is the same as in the second print statement.

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    26
    Well, for example:

    Code:
    Code:
    int main( void )
    {	
    	char 
    		buf[ 1024 ], 
    		* ptr = buf;
    	printf("sizeof( buf ): %d\n", sizeof( buf ) );
    	printf("sizeof( ptr ): %d\n", sizeof( ptr ) );
    	return 0;
    }
    And when you pass an array to a function, it resolves to a pointer, so the effect is the same as in the second print statement.
    I understood ur example but still wondering as to y cant we use sizeof in the above memset since
    Code:
    sizeof(abc_ca)
    wuld return 3 I guess..

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I understood ur example but still wondering as to y cant we use sizeof in the above memset since

    The point is simply that you can use it on 'buf' but not 'ptr'. And inside a function, you'd have the same problem:

    Code:
    void test( char buf[ 1024 ] )
    {	
    	printf("sizeof( buf ): %d\n", sizeof( buf ) );
    }

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    26
    Thanks Sebastiani...Now I get it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption/Decryption Help
    By coolage in forum C++ Programming
    Replies: 9
    Last Post: 04-25-2008, 01:53 AM
  2. To generate bar code from a number
    By darkducke in forum C Programming
    Replies: 18
    Last Post: 01-16-2008, 06:33 AM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Compile time switch
    By Roaring_Tiger in forum C Programming
    Replies: 13
    Last Post: 09-12-2004, 01:16 PM
  5. Encryption Algorithms
    By face_master in forum C++ Programming
    Replies: 15
    Last Post: 06-18-2003, 01:28 PM

Tags for this Thread