0

I have a char array and I'm iterating through via the pointer and trying to convert it to int, but it doesn't seem to be working e.g when I have "1", it gets converted to 50 instead of just 1.

 char infix[SIZE];
      gets(infix);

while(*infix){
int temp = (int) (*infix);

infix++;
}

https://pastebin.com/8NMkUQw4

  • Never ***ever*** use `gets`. It's so [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) it has even been removed from the C standard. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Oct 15 '20 at 10:25
  • As for your problem, there is no existing character encoding scheme where `'1' == 1`. You're probably using [ASCII](https://en.wikipedia.org/wiki/ASCII) in which `'1'` is equal to the integer `49`. However, if you study any ASCII [table](https://en.cppreference.com/w/c/language/ascii) (or the C specification) then you will learn that all digits are encoded contiguously, which means you can subtract `'0'` from any digit character and get the corresponding integer. As in e.g. `infix[0] - '0'` – Some programmer dude Oct 15 '20 at 10:27
  • I don't understand why are you iterating via pointer? What's wrong with iterating via index? – Rohan Kumar Oct 15 '20 at 10:27
  • 3
    Are you sure that is the exact code you compile? `infix++;` You cannot assign a new value to an array. – Gerhardh Oct 15 '20 at 10:27
  • @Someprogrammerdude Thank you, I learnt something new from you. Unfortunately it's a template given by my professor I cannot change that part. How do I convert to int instead of ascii representation? – lanav51095 Oct 15 '20 at 10:28
  • @RohanKumar I do not have the size and from what I'm told i should iterate via pointer and i don't have to write additional code to count – lanav51095 Oct 15 '20 at 10:29
  • @Gerhardh i added pastebin link – lanav51095 Oct 15 '20 at 10:29
  • @lanav51095 Then you need to tell your professor to get updated on what's been going for the last 20 or so years. The `gets` function have been known to be dangerous since the very beginning, was obsoleted in the C99 standard (21 years ago), and finally removed in the C11 standard (9 years ago). – Some programmer dude Oct 15 '20 at 10:31
  • Please add the correct code into the question. Your code is significantly different from `void expressionLL(char* infix, LinkedList *inExpLL)`. Arrays are not pointers and pointers are not arrays. – Gerhardh Oct 15 '20 at 10:31
  • @Gerhardh if you read the part I'm having issue with, it's the same except I stripped it.. because that's what i need help with – lanav51095 Oct 15 '20 at 10:32
  • The thing is, you didn't strip it but you changed meaning completely. How should we know what else is different from your real code? We only know that this is not the code you are using. That is why a [MCVE](https://stackoverflow.com/help/mcve) should be included in every question. Something only slightly related to your code creates more confusion than it help. – Gerhardh Oct 15 '20 at 10:35
  • @Gerhardh Sorry, I did not mean to change the meaning, i'm new to c so thanks for pointing out.. – lanav51095 Oct 15 '20 at 10:37

1 Answers1

1

As pointed out in second comment, what you're getting ASCII value of '1' character. What you'll need to do is something like this:

int temp = (int) (*infix - '0');

Also, you should use fgets() for taking string input. I wasn't able to compile your code, so I made some minor changes. Here is what I tested:

#include <stdio.h>
#include <string.h>

int main() {
    char infix[10];
    fgets(infix, 10, stdin);
    infix[strlen(infix)-1] = '\0';

    char *pArr = infix;

    while (*pArr != '\0') {
        int temp = (int) (*pArr - '0');
        printf("%d\n", temp);
        pArr++;
    }
}

When I ran this, I was getting expected output:

src : $ ./a.out 
123
1
2
3
Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40