What is wrong with this c strdup code?

Posted by bstullkid on Stack Overflow See other posts from Stack Overflow or by bstullkid
Published on 2010-05-07T13:09:34Z Indexed on 2010/05/07 13:18 UTC
Read the original article Hit count: 178

Filed under:
|

Consider this code:

char *strs[] = { "string1", "string2", NULL };
char *ptr1 = NULL, *ptr2 = NULL, *tmp;
short iter = 0;

tmp = ptr1;
while (iter < 2)
{
   tmp = strdup(strs[iter]);
   tmp = ptr2;
   iter++;
}

printf("1: %s\n2: %s\n", ptr1, ptr2);

I want this to output "string1\nstring2\n" however str1 and str2 remain null. What am I doing wrong?

© Stack Overflow or respective owner

Related posts about c

    Related posts about strdup