Results 1 to 4 of 4

Thread: String to int conversion...

  1. #1
    Junior Member
    Join Date
    Jan 2008
    Answers
    8

    String to int conversion...

    Hello everybody!

    Please have look on this code.

    struct record{
    int num,password;
    char name[10];
    };

    struct record person[10]; //array of structure
    main(){
    func1();
    }

    void func1(){
    char *no,*p;
    int j,true_id=0;
    no=malloc(sizeof(char*));
    printf("\nperson.num: %c ",person[1].num);
    no=person[1].num; //assignes to array member value
    printf("\nno: %c ",no); //statement 1
    printf("strtol: %ld",strtol(no,&p,0)); //statment 2 // use of strtol
    free(no);
    }

    As the pointer 'no' can get number in string format, it should return long integer value of that number but i am getting here 0.

    The o/p result of statment 1 is '1'.
    And the o/p result of statment 2 is 0.?

    how will the strtol behave?


  2. #2
    Contributing Member
    Join Date
    Dec 2007
    Answers
    46

    Re: String to int conversion...

    This is a buggy program. Here is why:

    1.
    no = malloc(sizeof(char*)); // 4 bytes of characters

    Memory space of 4 bytes (32 bit machine) is being allocated by malloc.
    Let us say that the address returned by malloc is 1000.

    2.
    printf("\nperson.num: %c ",person[1].num);

    The array of structures is global initialized data. So, all the structre values are initialized to zero. So, person[1].num will be zero. From the Ascii table, the ASCII code for 0 is 0 (NULL). So, 0 is printed.

    3.
    no=person[1].num;

    This is a bug. Remember that no stores the address returned by malloc?? The above libe just wipes out the address and stores 0 in no i.e. we lost the malloc'ed address i.e. we have created a memory leak!!!

    4.
    printf("\nno: %c ",no);
    This statment will print the Ascii equivalent of zero i.e. zero (NULL).
    I do not know how you can get 1 as the answer here. Are you sure you tried this program out??

    5.
    printf("strtol: %ld",strtol(no,&p,0));
    Strtol(Ascii(0)) == Strtol(0) == 0

    6.
    free(no);
    This is another bug as there is nothing to free as the malloc'ed address i.e. 1000, has already been overwritten to 0.

    Hope that helps.


  3. #3
    Junior Member
    Join Date
    Jan 2008
    Answers
    8

    Re: String to int conversion...

    thank you. I found more bugs in addition with pointed out by u.

    I'll come back soon with proper code n' query.


  4. #4
    Junior Member
    Join Date
    Jan 2008
    Answers
    8

    Re: String to int conversion...

    struct record{
    int num,password;
    char name[10];
    };

    struct record person[2]={1,6,"ka", 2, 5, "123"};; //array of structure

    void func1();

    void main(){
    clrscr();
    func1();
    getch();
    }

    void func1(){
    char *no,*p;
    int j,true_id=0;
    //no=malloc(sizeof(char*));
    printf("\nperson.num: %d ",person[1].num);
    printf("\nperson.password: %d ",person[1].password);
    printf("\nperson.name: %s ",person[1].name);
    no=&person[1].name; //assignes to array member value
    printf("\nno: %s",no); //statement 1
    printf("\nstrtol: %ld",strtol(no,&p,0)); //statment 2 // use of strtol
    //free(no);
    }


    here is the repaired code. Though my string"123" get changed to "kavita", the strtol returns the same no.(I was expecting to change the long int value as my string changed)???


  5.  Sponsored Ads
    Ad


     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact