1

I am having a hard time understanding why in the following C program, I am not able to properly return an array of characters. The issue seems to be in the line:

results[indx][indx2]='X';
#include <stdio.h>
#include <string.h>

const char *PrintPlaceholder(char *words[], char *results[], int wordsNumber);


int main(){
    char *word[]={"Hello","Fun"};
    char *results[2];
    
    
    PrintPlaceholder(word, results,2);
    for (size_t indx=0; indx<2; indx++){
    printf("Output is %s\n",results[indx]);
    
    }
    
    
    return 0;
}



const char *PrintPlaceholder(char *words[],char *results[], int wordsNumber){
        
        size_t wordLength;
        for (size_t indx=0; indx<wordsNumber; indx++){
            
                    
        wordLength=strlen(words[indx]);
        printf("Word length is %d\n",wordLength);
        
        for (size_t indx2=0; indx2<wordLength; indx2++){
            printf("2nd for loop");
            
            results[indx][indx2]='X';
            printf("Char is %c ",results[indx][indx2]);
        }
        printf("\n");
        
        }
            
    
    }

Many thanks

Oriol
  • 75
  • 5
  • `results[indx]` is dereferenced without being initialized. – MikeCAT Jul 29 '21 at 13:17
  • @MikeCAT. Initially 'results' is an empty strings array and I was expecting that after the function call, such array could be filled with some values. I did this because there is no way to return the strings array results from the call so I intended to call such array by reference in the function call – Oriol Aug 01 '21 at 16:11

0 Answers0