Character array pointer

This is a discussion on Character array pointer within the C Programming forums, part of the General Programming Boards category; Code: I am trying to store tokens in the 'arr' array but it prints wrong stuff... do you mind also ...

  1. #1
    axe
    axe is offline
    Registered User
    Join Date
    Jan 2006
    Posts
    33

    Character array pointer

    Code:
    I am trying to store tokens in the 'arr' array but it prints wrong stuff... do you mind also explaining why it prints garbage?
    
      char *arr[30000];
      const char *delimiters=";,.1234567890 ";
      char *temp;
      int k;
      temp=strtok(file_data,delimiters);
      while(temp != NULL)
      {
        printf(" %s ", temp);
        k++;
        get=temp;;
        arr[k]= (char*)temp;
        temp=strtok(NULL,delimiters);
        printf("haha!! %s hoho\n",arr[k]);
      }

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    19
    Quote Originally Posted by axe View Post
    Code:
    I am trying to store tokens in the 'arr' array but it prints wrong stuff... do you mind also explaining why it prints garbage?
    
      char *arr[30000];
      const char *delimiters=";,.1234567890 ";
      char *temp;
      int k;
      temp=strtok(file_data,delimiters);
      while(temp != NULL)
      {
        printf(" %s ", temp);
        k++;
        get=temp;;
        arr[k]= (char*)temp;
        temp=strtok(NULL,delimiters);
        printf("haha!! %s hoho\n",arr[k]);
      }
    in a quick look maybe because your forgot to initialize k to 0 ?

  3. #3
    axe
    axe is offline
    Registered User
    Join Date
    Jan 2006
    Posts
    33
    thanks.... that worked!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with character array pointer and strcmp
    By axe in forum C Programming
    Replies: 2
    Last Post: 11-13-2007, 07:47 PM
  2. Pros pls help, Pointer to array
    By kokopo2 in forum C Programming
    Replies: 7
    Last Post: 08-17-2005, 11:07 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. mygets
    By Dave_Sinkula in forum C Programming
    Replies: 6
    Last Post: 03-23-2003, 06:23 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21