1

I used this code to print some string,but it does not print any thing.
What is the problem?

char* getNotFilledEncryptionParams(void)
{
    char* nofilledStr;
    char tmp[3];
    const char * arr[]= {" P,"," Q,"," A,"," B,"," C,"," R,"," S0,","S1,","S2,","F1,","G1"};


   for(i=0;i<11;i++)
   {
      if(filledParams[i] == 0)
      {
        strcpy(tmp,arr[i]);
        strcat(nofilledStr,tmp);
      }
   }

return nofilledStr;
}

Usage:

int main(void){

    char *remaining;

    remaining = getNotFilledEncryptionParams();
    printf("\r\n Remaining item:%s",remaining);

}

I think the problem is in const char * arr[] and I changed it,but the problem remains.

Hamid
  • 1,493
  • 2
  • 18
  • 32
  • 1
    For one thing, tmp[3] should be at least 4 characters wide to accomodate the NULL when you use strcpy(). What is filledParams? – clarasoft-it Jul 28 '16 at 19:54
  • that is an integer array . @clarasoft-it – Hamid Jul 28 '16 at 19:57
  • 1
    Dragging the strings to append through this `tmp` variable is useless. You can directly append `arr[i]` to the result string once you have allocated space for it. – Thomas B Preusser Jul 28 '16 at 19:57
  • @clarasoft-it: `NULL` is a null pointer constant. You mean the null character, `'\0'`. – Keith Thompson Jul 28 '16 at 19:58
  • 2
    Regarding the title, I'm fairly sure this has nothing to do with any editor. – Keith Thompson Jul 28 '16 at 19:59
  • @keith Thompson Yes, you are right, I meant the null byte which has value zero (a char is really an integer). – clarasoft-it Jul 28 '16 at 20:09
  • There are so many mistakes in this code, really you should learn the basics of C before continuing with whatever you're doing. [Here is a list of resources](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – M.M Aug 15 '16 at 21:06

2 Answers2

4
  1. You didn't allocate any memory for noFilledStr, so its value is indeterminate and strcat(noFilledStr, tmp) is undefined.

    Use malloc to allocate memory and initialize noFilledStr with the returned pointer:

    char* noFilledStr = malloc(number_of_bytes);
    
  2. The strings in arr are char[4], not char[3] (do not forget the null byte!). tmp is too small to hold them, so strcpy(tmp, arr[i]) writes out of bounds.

cadaniluk
  • 15,027
  • 2
  • 39
  • 67
  • can I replace char* noFilledStr to char noFilledStr[30] ? – Hamid Jul 28 '16 at 19:56
  • Also, you would be returning data that isn't allocated anywhere, when you create local data in a function, it is temporary and only created on the stack. If you want to return the string then you should allocate the memory and return the start address of the allocated memory. – SPlatten Jul 28 '16 at 19:57
  • 1
    @Hamid No, because you return a pointer to it from the function and the automatic array `char noFilledStr[30]` would have ended its lifetime then. Read [this](http://stackoverflow.com/questions/4824342/returning-a-local-variable-from-function-in-c). – cadaniluk Jul 28 '16 at 19:57
3

You are trying to build the string to return in the location pointed to by nofilledStr but this pointer is pointing somewhere as you do not initialize it. You could use a sufficiently large static char[] array if you do not have to deal with multiple threads. Otherwise, use malloc() and require the caller to free() the returned string when he is done with it.

Thomas B Preusser
  • 1,159
  • 5
  • 10