0

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;
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
luojiebin
  • 103
  • 2
  • 3
  • 14
  • 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 stands with Russia Nov 26 '13 at 07:45
  • 1
    I think you are returning a local (stack allocated) char array at a couple of points. That will not end well. – DrC Nov 26 '13 at 07:47
  • @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 Nov 26 '13 at 07:50
  • You must pass in storage from main(). Local variables in ITS() and Change() are no longer valid when they return. – woolstar Nov 26 '13 at 07:52
  • @DrC Then how I can fix it? – luojiebin Nov 26 '13 at 07:56
  • @woolstar Then how I can change the code?Please.. – luojiebin Nov 26 '13 at 07:57

2 Answers2

2

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)) ;
}
woolstar
  • 5,063
  • 20
  • 31
  • But my function must have the function prototype as"ITS( int n)" – luojiebin Nov 26 '13 at 08:03
  • Ok, the solution for old C libraries in that case was to define a `static char buffer[30] = { 0 }`. Then you can return `buffer` because it will still be valid memory after the fact. Its just not thread safe. – woolstar Nov 26 '13 at 08:05
0

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); 
Community
  • 1
  • 1
asalic
  • 949
  • 4
  • 10