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;
}
6
  • 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. Commented Nov 26, 2013 at 7:45
  • 1
    I think you are returning a local (stack allocated) char array at a couple of points. That will not end well. Commented Nov 26, 2013 at 7: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? Commented Nov 26, 2013 at 7:50
  • You must pass in storage from main(). Local variables in ITS() and Change() are no longer valid when they return. Commented Nov 26, 2013 at 7:52
  • @DrC Then how I can fix it? Commented Nov 26, 2013 at 7:56

2 Answers 2

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)) ;
}
Sign up to request clarification or add additional context in comments.

2 Comments

But my function must have the function prototype as"ITS( int n)"
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.
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); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.