0

I have a character array in C:

Array = {'7', '.', '8', '*', '3', '-', '4', '.', '5', '6', '+', '1', '0'}

The above is a maths equation 7.8 * 3 - 4.56 + 1 where I have stored each character as a single element in the array.

I am trying to change the array so that the decimal numbers are one element. So, the array should look like:

Array = {'7.8', '*', '3', '-', '4.56', '+', '1'}

I would appreciate it if somebody could show me how to do this clearly as I am a beginner.

I am new to C so I apologise if the question doesn't make complete sense. Thanks for any help in advance!

Jasmine078
  • 389
  • 2
  • 4
  • 16
  • 3
    You realise then it wouldn't be a character array right? `'7.8'` is not a valid character. `"7.8"` is a valid string. You would need it to be an array of strings (`char*`). – John3136 Dec 06 '18 at 21:53
  • @John3136 An array of string would be acceptable. I apologise for not making it clearer. What would be the best way to do this? – Jasmine078 Dec 06 '18 at 21:55
  • @lurker `'7.8'` is not an error (let alone a syntax error), but probably won't do what OP expects. [See here](https://stackoverflow.com/a/7755280/1505939) – M.M Dec 06 '18 at 22:27
  • 1
    what you are actually trying to do is 'parse' the string. This is a much more complex task than 'how to I group things as chars / strings'. The classic way to do this is using yacc or bison. I mean - how do you know if the output should be "7", ".8*", "3-4" or "7.8","*","3" etc. A human knows but your code needs to understand the syntax rules of arithmetic – pm100 Dec 06 '18 at 22:31
  • @MM yes I used a bad example. – lurker Dec 07 '18 at 03:19

2 Answers2

1

C do not support Strings. You can use char arrays in order to implement them, for example:

#define MAX_SIZE 50
char expression[MAX_SIZE];
scanf("%s", expression); // input the expression from the user

The main problem is you cannot write "7.5" in C because char is bounded to 1 letter. Notice the define I used in order to bound the array length, you must declare arrays with a constant number.

If you want to make an array like that:

Array = {'7.8', '*', '3', '-', '4.56', '+', '1'}

That array will be an array of char [] which is: char [][] typed, an array of "strings".

let me know if that helped you :)

Elad Aharon
  • 405
  • 2
  • 18
  • 1
    Good detailed answer! To create the strings I'd create a float out of them, and cast it to char* – Josef Ginerman Dec 06 '18 at 22:03
  • `Array = {` ... is a syntax error, it would be better to show code of what you are suggesting at the end there. Also you do not need to specify the array bound if it has initializers – M.M Dec 06 '18 at 22:31
  • I've just copied the second line of code from the question to make it clearer to @Jasmine078 of course it's a syntax error. as I said the right way to implement that will be an array of arrays of chars. char* expression[MAX_SIZE]; will be a good decleration. also can be char** for dynamic allocations. – Elad Aharon Dec 06 '18 at 22:38
1

If I understand you correctly you just want:

const char *array[] = { "7.8", "*", "3", "-", "4.56", "+", "1"};
// well, short usage example:
for (size_t i = 0; i < sizeof(array)/sizeof(*array); ++i) { 
     printf("array[%zu] = %s\n", i, array[i]);
}

array is an array of pointers to constant characters. Each element of this array is a string literal. String literals are immutable in C, so it has to be pointers to const characters (you can remove the const, it's only for the compiler. Modifying the memory of a string literal is undefined behavior).

KamilCuk
  • 120,984
  • 8
  • 59
  • 111