c - Reference to a pointer is lost -
this question has answer here:
i trying understand why statement doesn't work.
char resp[] = "123456789"; void getvalue(char *im) { im = resp; printf("\n%s\n",im); } int main(int argc, char *argv[]) { char imei[11] = {0}; getvalue(imei); printf("\nimei: %s\n",imei); return 0; }
output:
123456789 imei:
you can not assign =
, use strcpy
instead:
#include <stdio.h> #include <string.h> char resp[] = "123456789"; void getvalue(char *im) { im = strcpy(im, resp); printf("\n%s\n",im); } int main(int argc, char *argv[]) { char imei[11] = {0}; getvalue(imei); printf("\nimei: %s\n",imei); return 0; }
that's because imei
array[11] (not pointer to), if want assign via =
can:
#include <stdio.h> char resp[] = "123456789"; void getvalue(char **im) { *im = resp; printf("\n%s\n",*im); } int main(int argc, char *argv[]) { char *imei; /* not array pointer */ getvalue(&imei); printf("\nimei: %s\n",imei); return 0; }
Comments
Post a Comment