Initializing character arrays with memset

This is a discussion on Initializing character arrays with memset within the C Programming forums, part of the General Programming Boards category; Hi, I have just realized that I was passing a pointer to pointer to memset function to initialize a character ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    Initializing character arrays with memset

    Hi,

    I have just realized that I was passing a pointer to pointer to memset function to initialize a character array but everything still seems to be working. Here is the "problematic" code:

    char myArray[25];
    memset(&myArray, 0, 25);
    Notice the & operator in front of myArray in memset call. It should not work but it does????

    The code should be without the & operator in front of myArray variable:
    char myArray[25];
    memset(myArray, 0, 25);

    Does anybody know why the initial code is working with the unnecessary & operator?
    Thank you in advance.

    Mark

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    It's just a fluke. The label myArray resolves to a pointer to some data on the stack, but since a pointer wasn't actually declared, the address of the label must also resolve to the same address (although I'm not sure if this is guaranteed by the standard). Had this been a pointer to the array, it would have been a different story, eg:

    Code:
    int main( void )
    {
    	char 
    		buf[ 256 ], 
    		* ptr = buf;
    	printf( "Value of 'buf': 0x%x\n", buf );
    	printf( "Address of 'buf' 0x%x\n", &buf );
    	printf( "Value of 'ptr': 0x%x\n", ptr );
    	printf( "Address of 'ptr' 0x%x\n", &ptr );
    	return 0;
    }
    So in short, you got lucky.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you're just initializing the whole array to 0, then here's an easier way:
    Code:
    char myArray[25] = {0};
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    Initializing character arrays with memset

    Hi,

    I have just realized that I was passing a pointer to pointer to memset function to initialize a character array but everything still seems to be working. Here is the "problematic" code:

    char myArray[25];
    memset(&myArray, 0, 25);
    Notice the & operator in front of myArray in memset call. It should not work but it does????

    The code should be without the & operator in front of myArray variable:
    char myArray[25];
    memset(myArray, 0, 25);

    Does anybody know why the initial code is working with the unnecessary & operator?
    Thank you in advance.

    Mark

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I'm not sure why you reposted this. You should post further comments here.

  6. #6
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    The address of an array and the address of its first element (which is what you get when you leave off the &) are the same place, although different types. There's nothing inherently wrong with taking the address of an array, but it's usually not what you want. It works in this case because you don't care about the type (it's converted to void* either way).

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Threads merged.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. scanf(), Parameter Passing, and Character Arrays
    By linguae in forum C Programming
    Replies: 2
    Last Post: 05-02-2005, 04:19 AM
  4. multidimentional character arrays
    By ckeener in forum C++ Programming
    Replies: 7
    Last Post: 03-11-2005, 07:49 PM
  5. Assign Character Arrays data from an STL Container
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 12-05-2001, 09:33 AM

Tags for this Thread


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