Reputation: 73
I know, there are some solved solutions here on stackoverflow. But I don't know why doesn't mine one work.
char *ink[90]
strcpy(ink, "hi");
printf("stepiii %c \n",ink[0] );
printf("ascii %d\n",ink[0] );
Instead of "ascii 104" returns bad value like
"ascii 26984"
What am I doing wrong?
Upvotes: 0
Views: 63
Reputation: 134286
In your code,
char *ink[90]
is wrong. You don't need an array of pointers, as seen in the rest of the snippet. Change that to
char ink[90]= {0};
In case you're wondering about the previous output, that code invoked undefined behavior. So, the output is essentially meaningless and MUST not be trusted.
Upvotes: 3
Reputation: 53006
You are creating an array of pointers and copying characters to it. You need an array of char
and copy the characters to it, like this
char ink[90]
strcpy(ink, "hi");
printf("stepiii %c \n", ink[0]);
printf("ascii %d\n", ink[0]);
What you had is wrong because ink[0]
is the address of the first pointer in the array, you initialized the array with strcpy()
which should trigger an incompatible pointers type warning, enable compilation warnings to avoid this.
The way it is in your code it invokes undefined behavior and your program could crash or anything could happen. The printed value doesn't make sense.
Upvotes: 2