-
copying character arrays
I'm trying to find out what are the first and second characters in the string. I have a character array token[1] where each index contains a separate string. I want to look inside token[1] for the characters. My idea was to copy token[1] into dir[]. Is this logic correct? If not can you please advice. In this example, I'm trying to copy a string (e.g. "hi") located in token [0] into another array, dir, so that I can later traverse through dir to find specific characters.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char *tokens[513];
char *dir;
tokens[0] = "Hi";
strcpy(dir, tokens[1]);
printf("message: %s" , tokens[0]);
}
or is there a way to directly look into tokens? Please help!
-
>is there a way to directly look into tokens?
Provided you don't try to modify any of the strings in tokens you can look all you want just by iterating the same way you would an array.
>strcpy(dir, tokens[1]);
This won't work because dir is declared as a char * but no memory is allocated to it. Either declare dir as an array or allocate memory to it with malloc.
-Prelude