-6

I have some code like this:

int main(){
    char *a = "2d6c002d61";
    char *b[strlen(a)];
    char *p;
    int count = 0;
    p = strtok(a,"00");
    while(p){
        unsigned char *c;
        char tar[100];
        hex_to_ascii(p,c);
        strncpy(tar,c,2);
        tar[2] = '\0';
        b[count]=tar;
        count++;
        p = strtok(NULL,"00");
    }
    b[count] = NULL;
    return 0;
}

The hex_to_ascii() will convert hex string to ascii string, for example, "2d6c" will be converted to "-l". I have checked this function and make sure it worked.

I hope to split a to "2d6c" and "2d61", then use hex_to_ascii() to convert them, and make b = {"-l","-a"}.

The problem is, although I got -l and make b = {"-l"} at first, it became {"-a","-a"} after that.

ully
  • 7
  • 2
  • 1
    Your ` char tar[100];` is local to `while` loop will be destroyed every time. – kiran Biradar Sep 24 '18 at 08:07
  • 1
    `char tar[100]; tar = somefunction(p);` is invalid. You can't assign to the name of an array. – Swordfish Sep 24 '18 at 08:08
  • Also I'm wondering how `tar = somefunction(p);` is not giving any warning/error. – kiran Biradar Sep 24 '18 at 08:08
  • 3
    `char *a = 2d6c002d61` is syntactically wrong, show us the actual code. – Sourav Ghosh Sep 24 '18 at 08:08
  • `char *a = 2d6c002d61` looks very suspiciuos. 1. Semicolon (`;`) is missing. 2. Hex-numbers have to be prefixed with `0x`. 3. Hard-coded address values should be used in special cases only (e.g. to access special H/W address)... – Scheff's Cat Sep 24 '18 at 08:09
  • Even if the code was `char *a = 0x2d6c002d61;`, that's also not valid C. See [“Pointer from integer/integer from pointer without a cast” issues](https://stackoverflow.com/questions/52186834/pointer-from-integer-integer-from-pointer-without-a-cast-issues). – Lundin Sep 24 '18 at 08:11
  • you cannot do this `b[count] = tar;` – torstenvl Sep 24 '18 at 08:24
  • Please check how to write a [mcve] – Kami Kaze Sep 24 '18 at 08:31
  • @ torstenvl Would you please explain a little more? if I can do 'b[1] = "sleep" ' why I cannot do 'b[count] = tar'? – ully Sep 24 '18 at 08:39
  • And it is also quite usefull if you describe what you want to do with the problem. That would make helping you easier. Other than that I appreciate that you take your time to correct the problems in your question. – Kami Kaze Sep 24 '18 at 08:39
  • I think you accidentially changed the title of the question. – Kami Kaze Sep 24 '18 at 09:00

1 Answers1

0

You can not create multiple arrays in a loop.

For what you want to do, you should use malloc and not arrays. Don't forget to free after you are done with it.

With malloc you can get some memory for your free usage and it only gets invalid when it is freed.

What happend in your code was:

You create tar in your loop and the whole array gets invalid the moment you reache the } at the end of one iteration of the while loop. That means in every iteration of the loop you create a new array (maybe even in the same place as before). The old one is invalid but you saved the address of the start of the array in b, you are not allowed to read from that address making it useless.(and creating undefined behaviour when you try).

If you want specfic addresses of parts of the array you can use &tar[x]. If you use &tar[0] it is clearer that you are dealing with an array. And would need a seprate string for every iteration of the loop.

Regarding to the question in your comment.

"hello" is a string literal, that means it is unmutable and the expression returns an address/pointer. This address can be assigned to a pointer variable. The string has static storage duration (it is valid until the program terminates). But if you assign tar it has automatic storage duration, which basically means it is only valid until you reache the }of the block you are currently in.

Interesting read on how to use malloc in c: Do I cast the result of malloc?

Kami Kaze
  • 2,069
  • 15
  • 27