Thread: extract numbers from char array into array of pointers

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    82

    extract numbers from char array into array of pointers

    i need to extract numbers from user input. i think it's best to extract them into array of pointers because in that case i can refer to the numbers with their index.

    i can't assign an int (which i extracted from a char array) to char* a[];

    how should i do that?

    the code i've written so far, this is just a piece of the actual code:

    Code:
    #include <stdio.h>
    #define SIZE 100
    
    int getCurrentNumber(int i) {
      . . .
      return res;
    }
    int main() {
    
    
      std::cout << "Enter expression: ";
      fgets(expr, SIZE, stdin);
    
    
      char* simplified[SIZE];
      int i=0;
      int j = 0;
    
    
      simplified[j++] = getCurrentNumber(i);
    
      return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Why not extract them to an array of int's then?

    Presumably, you want int's at some point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    well, the user input is a math expression, so i wanna separate the numbers and the operations, like this:

    Code:
    char* a[] = {"566", "+", ...}

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So getCurrentNumber() is actually mis-named and you perhaps want to get the next token?

    A token being either the string representation of a number, such as "123" or an operator such as "+"

    If everything is space separated, you might look at strtok(), but read all the warning labels before proceeding.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    With strtok I guess I could assign char*'s to array of ptrs, but I should define a whole list of delimiters so its not convenient. And no, everything is not space separated.

  6. #6
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    You might be better off writing a parser/tokenizer yourself then. Your input data will be a c-string, and your output data will be a series of tokens you can work with.
    Since, presumably, you want to evaluate that math expression at some point, you may want to write another procedure that will take a series of tokens and using some predefined rules (operator precedence, unary/binary nature of operators, brackets, etc) make that into, say, abstract syntax tree, which may be used as input data in yet another procedure that will go through and evaluate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 39
    Last Post: 01-20-2015, 03:42 PM
  2. Howto extract a positive integer from an unsigned char array
    By GregoireLeGros in forum C++ Programming
    Replies: 13
    Last Post: 01-14-2015, 02:14 AM
  3. how to extract int from char array
    By amzc in forum C Programming
    Replies: 3
    Last Post: 11-26-2012, 12:25 PM
  4. Extract char array into int vars
    By metros in forum C Programming
    Replies: 5
    Last Post: 02-26-2010, 10:42 PM
  5. Extract numbers as integers from a string array
    By DarthC in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2002, 05:09 PM

Tags for this Thread