0
votes

I am just writing a function which works like IntegerToString using recursion,but the GCC just comes

"its.c: In function ‘ITS’:
its.c:26:3: warning: initialization makes integer from pointer without a cast [enabled by default]
its.c:26:3: warning: (near initialization for ‘buffer[0]’) [enabled by default]
its.c:26:3: warning: initialization makes integer from pointer without a cast [enabled by default]
..."

I don't know how to fix with it.Please help me.

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

char *Change(char *buffer,int n)
 {
   if(n==0){
    return(buffer);
  }else{
    int left=n%10;         
    n=n/10;                
    int len=strlen(buffer);
    buffer[len-1]='\0';    
    char new_array[30]={NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
            NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
            NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
    printf("%s\n",new_array);
    new_array[0]=left+'0';   printf("NEW:%s\n",new_array);
    strcat(new_array,buffer); printf("TEST:%s\n",new_array);
    return(Change(new_array,n));
  }
}

char *ITS(int n)
{
  char buffer[30]={NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
           NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
           NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};;
    return(Change(buffer,n));
}

int main()
{
   int n=1729;
   char *buffer=(char*)malloc(30*sizeof(char));
  if(buffer==NULL)
     {
      printf("Malloc Fault!\n");
      exit(-1);
     }
   buffer=ITS(1729);
   printf("%s\n",buffer);

  return 0;
}
2
Why are you trying to initailize char objects with NULL? NULL is intended to be used with pointers. char is not a pointer, it is an integer. - AnT
I think you are returning a local (stack allocated) char array at a couple of points. That will not end well. - DrC
@AndreyT Because I want to replace the rubbish value with nothing in the array.I initailize the array new_array and printf it and get"]�",it's a rubbish value.I want to replace.So how I can fix ti? - luojiebin
You must pass in storage from main(). Local variables in ITS() and Change() are no longer valid when they return. - woolstar
@DrC Then how I can fix it? - luojiebin

2 Answers

2
votes

As its been pointed out in the comments, its not safe to use and return local storage from the functions. Change the signatures for ITS() and Change() so that they take a buffer from main():

char * Change(char * srcBuffer, int n) ;
char * ITS( char * srcBuffer, int n) ;

int main()
{
  int n= 1729 ;
  char buffer[30] ;
  printf("%d = %s\n", n, ITS( buffer, n)) ;
}

If you are clever, then you can have Change return a different value than ITS does, and use it to tell each level where the next digit goes.

If you are restricted on the signature for ITS (see comments), then use static memory in ITS. Its not thread safe, but it will work otherwise:

char * Change(char * srcBuffer, int n) ;
char * ITS( int n )
{
  static char buffer[30] = { 0 } ;
  return Change( buffer, n) ;
}

int main()
{
  int n= 1729 ;
  printf("%d = %s\n", n, ITS( n)) ;
}
0
votes

Do not initialize the array of chars with NULLs (even if NULL is actually... 0 or When was the NULL macro not 0?). NULL is used to show that a pointer points to "nothing". If you want to init the array with 0 you can use memset (C style):

BUFFER_LEN = 30;
memset(buffer, 0, sizeof(*buffer)*BUFFER_LEN);