Thread: pass array to memset

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    5

    Question pass array to memset

    Hi,

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char myArray[] =
    "\x31\x32\x31\x32\x31\x32x31\"
    "\x32\x32\x43\x54\x65\x78\x6e\x61";
    
    int main(void)
    {
        char myMem[225];
        memset(myMem, &myArray, 15);
        return(0); 
    }
    I'd like to pass the myArray to memset's 2nd parameter, that is, replicate myArray 15 times inside myMem[]. Is it doable?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You mean like strcpy? Or like memmove?


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

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    5
    Quote Originally Posted by quzah View Post
    You mean like strcpy? Or like memmove?
    Quzah.
    Neither. I'm going to use
    Code:
    memcpy(&myMem[225-strlen(myArray)],myMem,strlen(myArray));
    But the problem I encounter is on the memset();
    I don't know how to pass an array as second argument.

    memset(myMem, ???myArray???, 15);

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You mean like strcpy or memmove?


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

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    memset is not able to do this. As far as I'm aware, there is no generic function in C that would copy a block of memory x number of times.

    You can of course write a function that takes the relevant char * arguments and duplicates it the necessary number of times. You could for example use a loop using memcpy().

    By the way, would it not be easier to write your string as "121212122CTNA", rather than the hex notation? [If you think that is actually hiding some sort of password or such, I can guarantee that looking at the memory in a debugger or using the "strings" command in Linux/Unix will show the same thing whether you wrote it in clear-text or as hex-codes, so it's only (but not very) meaningful to be cryptic about it in the source file, and it only makes it hard for someone who don't know the ascii table or don't understand hex - and those people would probably barely recognize a piece of C code as something readable anyways!].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    As noted call memcpy(), iteratively or recursively, "x" number of times to populate the destination array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pass struct array to function
    By aditya_t90 in forum C Programming
    Replies: 4
    Last Post: 03-30-2009, 11:54 AM
  2. pass 3D array
    By gnncj in forum C Programming
    Replies: 3
    Last Post: 07-24-2005, 03:44 AM
  3. pass the pointer of a two dimension array to a function
    By SoFarAway in forum C Programming
    Replies: 8
    Last Post: 04-13-2005, 05:43 AM
  4. Replies: 3
    Last Post: 03-23-2005, 12:22 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM