memcopying to a variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • boddah
    New Member
    • Dec 2008
    • 15

    memcopying to a variable

    Hi guys, I'm trying to copy the output from the for loop to a variable 'temp'

    for(int i=1;i<4;i++)
    {
    printf("%s", 2 * i); //outputs - 246
    }
    printf("\n");

    ----------------------------------------------------------
    I've tried this to only get 'segmentation fault'

    char temp[20];
    int a;
    for(int i=0;i<10;i++)
    {
    printf("%d", 20 * i);
    memcpy(temp[a], 2 * i, 1);
    a++;
    }
    printf("\n");

    ---------------------------------------------------------
    can anyone help me? Thanks
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Originally posted by boddah
    Code:
    for(int i=1;i<4;i++)
    {
          printf("%s", 2 * i); //outputs - 246
    }
    With the "%s" format code you promise printf that you will pass it the pointer to a null-terminated string. Instead, you pass it an integer value. It will treat that integer value as a pointer and try to peer into memory at that address -- most likely something bad will happen.

    Originally posted by boddah
    Code:
    char temp[20];
    int a;
    for(int i=0;i<10;i++)
    {
          printf("%d", 20 * i);
          memcpy(temp[a], 2 * i,  1);
          a++;
    }
    Good -- you're using the "%d" format string in printf. However, you are violating the function prototype for memcpy. You should have gotten compiler errors. If you're not, then perhaps you're not including <string.h>.

    What are you trying to accomplish with the memcpy?

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      memcpy?

      Why not just:

      Code:
      temp[i] = 2* i;
      ?

      BTW: in your snippet the variable a is not initialized. Plus, your loop does not fill the entire array.

      Comment

      • boddah
        New Member
        • Dec 2008
        • 15

        #4
        char temp[20];
        int a=0;
        for(int i=0;i<10;i++)
        {
        printf("%d", 20 * i);
        memcpy(temp[a], 2 * i, 1);
        a++;
        }
        Hi again. Sorry I forgot to include the initialization of a=0 in the 2nd snippet. Actually the for loop will printf an output of something like this printf("%c%c", <some formula here>);
        the for loop doesn't iterate at a fixed num so I'm just initializing the temp[20] for testing purposes. What I'm trying to achieve are; while the for loop outputs the printf("%c%c,<f ormula>) -> outputting 2 characters each time it loops, I wish to put everything in 1 variable or 1 array of character. Hope that fills you in. Thanks

        Comment

        • newb16
          Contributor
          • Jul 2008
          • 687

          #5
          Use sprintf to create a string with a representation of a number, and strcpy to append it to your array.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            That's fine, but why the memcpy?

            A char is an integer. Your i is an integer. The calculation produces an integer. You assign your integer result to an integer.

            Just assign the result to your array element. The compiler does the math for you.

            You can assign temp[i] and temp[i+1] on the same loop cycle if you are trying to make your array look like your printf output.

            Comment

            Working...