-1

I am trying to cast a string to an int in C and I found really nice examples in the internet, like on: Converting string to integer C

OR:

   char *A = "123";
   int = A - '0';

But whatever solution I use, when I do it char to char in my program I get that warning.

So I first have a char array I split into separate parts. It looks like "1 2 3 4", and then I want to cast them to int:

char *a = "1 2 3 4";
char deli[] = " ";
char *ptr;
ptr = strtok(a, deli);
while (ptr != NULL) {
    int num = ptr - '0'; // or any other method which is casting the part now to an int causes the warning
    ptr = strtok(NULL, deli);
}

Does someone see what am I doing wrong here?

Community
  • 1
  • 1
malajedala
  • 867
  • 2
  • 8
  • 22
  • 3
    `a` is a string literal (read only), you can't use `strtok` over it, change to `char a[] = "1 2 3 4";` – David Ranieri Oct 03 '14 at 08:20
  • 7
    --> `int num = *ptr - '0';`, also --> `char a[] = "1 2 3 4";` – BLUEPIXY Oct 03 '14 at 08:20
  • possible duplicate of [Why do I get a segmentation fault when writing to a string?](http://stackoverflow.com/questions/164194/why-do-i-get-a-segmentation-fault-when-writing-to-a-string) – WhozCraig Oct 03 '14 at 08:22
  • 1
    Apart from being syntactically incorrect, `int = A - '0';` (fixed to, say, `int x = A - '0';`) would not work; you need `int x = *A - '0';` as one possibility, or (much less plausibly) `char *x = A - '0';`. – Jonathan Leffler Oct 03 '14 at 08:25
  • 1
    silly me, looking past the warning and directly into the blinding light of the pending seg-fault. – WhozCraig Oct 03 '14 at 08:26
  • Ok so I need to do *A - '0', can I ask why? – malajedala Oct 03 '14 at 08:27
  • `*A - '0'` subtracts one `char` value (`'0'`, which is actually an `int` in C, but it doesn't matter) from another (`*A`, which would be `'1'`). The difference is `1`. The alternative expression `A - '0'` will subtract 48 from the pointer value of `A`, which is totally undefined behaviour; there's no knowing exactly where that result pointer points, and the result might not even be valid. – Jonathan Leffler Oct 03 '14 at 08:30
  • That was a quick example, in my code I read a file line by line and then use strtok, so keep your comments to yourselves – malajedala Oct 03 '14 at 08:31
  • 2
    @malajedala: do not waste _our_ time by showing us an approximation to your code! We cannot help you if you don't show us the exact code that is causing trouble. Your compiler error message already doesn't make sense in the context of the code fragment you've shown. Please — read up on how to create an MCVE ([How to create a Minimal, Complete, and Verifiable Example?](http://stackoverflow.com/help/mcve)) and make sure that the code you show produces exactly the error message you claim! – Jonathan Leffler Oct 03 '14 at 08:33

1 Answers1

0

In C, not only single literal char like '0' are of type integer but you are also using in a numeral operation (substraction). Therefore, the expression A - '0' is of type numerical integer; which means that the value of the pointer A must be converted to an integer; hence the warning.

Like others have said in the comment, you need to convert the pointer A to the value of the first character to which it is pointing at: *A - '0' (for int i = *A - '0'). The same thing apply for the pointer ptr: int num = *ptr - '0'.

SylvainL
  • 3,926
  • 3
  • 20
  • 24