Thread: Converting string to integer

  1. #1
    Registered User
    Join Date
    May 2015
    Location
    Lucknow, U.P., India
    Posts
    9

    Converting string to integer

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    void St2Int() {
        char inp[10];
        printf("Input String number\n");
        scanf("%s", inp);
        int *p;
        p = (int *)malloc(strlen(inp));
        p = (int *)(inp);
        int res = *p;
        printf("%d\n", res);
    }
    I am trying to store the input string value in a char pointer first, then, caste it to an integer pointer and then save it in an integer variable, but I am getting wrong output.
    My method is stupid but I am a beginner and curious why this code is not working.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    it could be as simple as this

    Code:
        char str[] = "100";
        int number;
    
        sscanf( str, "%d", &number );
        printf( "number is - %d\n", number );
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    strtol() or strtoul() should be the preferred functions for converting strings to integers.

    These functions can detect numeric overflow, which sscanf cannot.
    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.

  4. #4
    Registered User
    Join Date
    May 2015
    Location
    Lucknow, U.P., India
    Posts
    9
    Quote Originally Posted by ssharish2005 View Post
    it could be as simple as this

    Code:
        char str[] = "100";
        int number;
    
        sscanf( str, "%d", &number );
        printf( "number is - %d\n", number );
    I didn't know about sscanf() function.
    But my question is, why is the variable res not storing the value p is pointing to, in fact, is p pointing to the input value at all?

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    inout string ("100" contains following chars
    Code:
    {'1', '0', '0', 0}
    for example

    in hex it will be
    Code:
    {0x31, 0x30, 0x30, 0x00}
    This is completely different from hex representation of number 100
    Code:
    {0x64, 0x00, 0x00, 0x00}
    So just casting pointer to first array of bytes and telling it to interpret the contents as integer instead of string will not magically replace contents of the array.

    You need change the contents of the memory not just the way the memory byte are read
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by amit_s95 View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    void St2Int() {
        char inp[10];
        printf("Input String number\n");
        scanf("%s", inp);
        int *p;
        p = (int *)malloc(strlen(inp));
        p = (int *)(inp);
        int res = *p;
        printf("%d\n", res);
    }
    I am trying to store the input string value in a char pointer first, then, caste it to an integer pointer and then save it in an integer variable, but I am getting wrong output.
    My method is stupid but I am a beginner and curious why this code is not working.
    I don't know if this is relevant or not but I don't think you even needed to malloc p anyway when all you do is immediately reassign the value of the pointer to a stack-based allocation and then store the value of the bytes there interpreted as an integer in a local variable. This would also create a memory leak that you can't fix because you overwrite the location of the heap allocation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting string to integer, for further use
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 07-01-2005, 03:12 AM
  2. Converting string to integer...
    By Ham in forum C++ Programming
    Replies: 6
    Last Post: 01-26-2003, 01:43 AM
  3. converting a integer to a string
    By LandMonster in forum C++ Programming
    Replies: 2
    Last Post: 12-21-2001, 12:17 PM
  4. Converting Integer to string
    By rm3 in forum C++ Programming
    Replies: 2
    Last Post: 12-18-2001, 02:24 PM
  5. Converting integer to string???
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 12-05-2001, 01:04 AM

Tags for this Thread