0

Here two prgrams present. Program 1 and program 2.

Input to both programs: {(0,0),(25,25)}

Output is : (0,0),(25,25)

The program -1 is working fine, giving out put as expected. Program 2 is not giving out put as expected bot no errors. Suspecting problem at *str2 = *token in program -2.But what the problem is not identified exactly. can any one explain me

Program --1

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

char*  fun_call(char [], int );

int main()
{
    char str[] = "{(0,0),(25,25)}";
    int base_str_leng;
    char *retun_ptr;
    base_str_leng = sizeof(str)-1;
    printf("base_str_lemg:%d\n",base_str_leng);
    retun_ptr =  fun_call(str, base_str_leng);
    printf("str2 in main loop:::%s\n",retun_ptr);
    return 0;
}

char* fun_call(char str1[], int len)
{
    printf("len::%d\n", len);
    len = len-2;
    printf("len=%d\n",len);
    char *token = str1;

    char *str2 = (char*) malloc( len * sizeof(char) );
    printf("sizeof(str2) = %zd\n",malloc_usable_size(str2));

    printf("token:%s\n", token);       
    printf("****While loop started****\n");
    int i=0,j=0;
    while(token[i] != '\0')
    {
          if(token[i] != '{' && token[i] != '}' )
          {
              printf("Each token:%c\n", token[i]);
              str2[j] = token[i]; 
              i++;
              j++;
              printf("str2_Token::%c\n", str2[j]);
          }
          else
          {
              i++;
          }

        }
        printf("Function str2::%s\n", str2);
        return (str2);
}

Program: 2

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

char*  fun_call(char [], int );

int main()
{
    char str[] = "{(0,0),(25,25)}";
    int base_str_leng;
    char *retun_ptr;
    base_str_leng = sizeof(str)-1;
    printf("base_str_lemg:%d\n",base_str_leng);
    retun_ptr =  fun_call(str, base_str_leng);
    printf("str2 in main loop:::%s\n",retun_ptr);
    return 0;
}

char* fun_call(char str1[], int len)
{
    printf("len::%d\n", len);
    len = len-2;
    printf("len=%d\n",len);
    char *token = str1;

    char *str2 = (char*) malloc( len * sizeof(char) );
    printf("sizeof(str2) = %zd\n",malloc_usable_size(str2));

    printf("token:%s\n", token);       
    printf("****While loop started****\n");
     while(*token != '\0')
     {
          if(*token != '{' && *token != '}' )
          {
              // printf("Each token:%c\n", token[i]);
              *str2 = *token; 
              str2++;
              token++;
              // printf("str2_Token::%c\n",*str2);
          }
          else
          {
              token++;
          }
        }
        printf("Function str2::%s\n", str2);
        return (str2);
}
gogaz
  • 2,323
  • 2
  • 23
  • 31
Raji
  • 21
  • 1

1 Answers1

0

In your program 2 you do not end the string with \0, your while loop ends when token[i] == '\0'

You could use calloc instead to initialize the memory to 0

char *str2 = calloc( len, sizeof(char) );
AndersK
  • 35,813
  • 6
  • 60
  • 86