Thread: strtok() and strcpy

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    3

    strtok() and strcpy

    hi, im just learning c and im trying to copy the token to a 2 dimension char array but the array( eq1[][] ) just gets filled with nulls. i cant figure out what im doing wrong...


    Code:
    void tokenize(void){
       extern char equation[];
       extern eq1[];
       int i = 0;
       char *token;
    
       token = strtok( equation, " ");
       while ( token != NULL ){
          printf( "token is %s\n", token);
          strcpy(eq1[i], token);
          token = strtok( NULL, " ")
          printf("eq is %s\n", eq1[i]);
          ++i;
       }
    }
    if anyone could help me out id really appreciate it

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Presumably you have eq1 defined in your function correctly? Right now it's not a two-d array for you to fill.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I pretty sure right now it is not even something that will pass a compiler:

    Code:
    extern eq1[];
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    3
    its just a typo, its actually declared like:

    extern char eq1[][];

    the strtok seems to work, the printf's work but wheni display eq1 i get (null)

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by grimlark View Post
    its just a typo, its actually declared like:
    So how many more typos do we have to deal with before we get to the real thing? Cut and paste the actual code you are using, or stop trying to waste other people's time.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    3
    its the only typo, and it is the actual code im using. i just cant figure out why im getting (null) in eq1 instead of token

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by grimlark View Post
    its the only typo, and it is the actual code im using. i just cant figure out why im getting (null) in eq1 instead of token
    Like we all didn't see the missing semicolon either.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by grimlark View Post
    its just a typo, its actually declared like:

    extern char eq1[][];

    the strtok seems to work, the printf's work but wheni display eq1 i get (null)
    Surely you need to specify at least the second of those array sizes, so that the compiler can determine where to store the strings.

    What is your eq1 defined as? My guess would be that you are mismatching the definition and the declaration, if the first suggestion I made isn't the cause of the problem.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strtok Problem.
    By sergioms in forum C Programming
    Replies: 8
    Last Post: 11-23-2008, 12:03 PM
  2. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 06:48 PM
  3. What's up with this strcpy?
    By fanoliv in forum C Programming
    Replies: 7
    Last Post: 06-19-2006, 05:24 PM
  4. Parsing Strings
    By Hunter2 in forum C++ Programming
    Replies: 29
    Last Post: 12-05-2004, 07:48 PM
  5. help with strtok
    By requiem in forum C Programming
    Replies: 5
    Last Post: 04-29-2003, 04:10 PM

Tags for this Thread