adding int to char array in C

BVtp

I'm trying to parse assembly commands, and there's a certain case I need to generate a random number and add that to a char array. Example:

mov #(random number), r0

I have char* srcOp

Now I tried doing something like that:

int i;
time_t t;

srand((unsigned) time(&t));
i = rand() % 100;
(*srcOp)='#';
// append here i to srcOp . Supopse i is 39 -> srcOp should contain '#39'

and now I need to append the value of 'i' to srcOp, but I either get a runtime error or a compilation error.

anita2R

Decide on the largest random number you will create. Size src0p to match the string representation of the largest random number +1 for '#'

sprintf will output a formatted string representation of your number. Here I have formatted the number with leading zeros.

int main (void)
{
    char src0p[6];
    int i;

    i = 999;
    sprintf(src0p, "#%04d", i);

    printf("%s\n", src0p);
}

The output looks like this: #0999

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related