Thread: copying character arrays

  1. #1
    Unregistered
    Guest

    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!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  3. Is this poor coding? copying arrays of doubles
    By Hansie in forum C Programming
    Replies: 12
    Last Post: 05-25-2007, 02:34 PM
  4. Comparing Character Arrays problem...
    By newy100 in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2003, 07:54 PM
  5. help with character arrays
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-28-2002, 06:13 PM