Home > Enterprise >  Returning array of chars in C function
Returning array of chars in C function

Time:10-15

I have tried so many ways of doing this and I cannot get it to work. My setup says to use char[] but everything I've researched has no information on using that. Here is my code below

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

char[] makeString(char character,int count)
{
    char finalString[count];
    strcpy(finalString,character);
    for(int i=1;i<=count;i  )
    {
        strcat(finalString,character);
    }
    return finalString;
}
printf("%s\n",makeString("*",5));

I'm trying to create a function that returns a string of the given character count amount of times. Any help is greatly appreciated.

My apologies if this is a very simple error, I mostly code in python so C is very new to me.

CodePudding user response:

Your code doesn't compile. If you want to return an array you do char * not char []. Local variables like finalString are out of scope after the function returns.

Here are 3 ways of doing it:

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

char *initString(char *a, char c, int n) {
    memset(a, c, n);
    a[n] = '\0';
    return a;
}

char *makeString(char c, int n) {
    char *a = malloc(n   1);
    memset(a, c, n);
    a[n] = '\0';
    return a;
}

int main(void) {
    printf("%s\n", memset((char [6]) { 0 }, '*', 5));

    char s[6];
    initString(s, '*', (sizeof s / sizeof *s) - 1);
    printf("%s\n", s);

    char *s2 = makeString('*', 5);
    printf("%s\n", s2);
    free(s2);
}

CodePudding user response:

Arrays are not a first-class type in C -- there are a lot of things you can do with other types that you can't do with arrays. In particular you cannot pass an array as a parameter to a function or return one as the return value.

Because of this restriction, if you ever declare a function with an array type for a parameter or return type, the compiler will (silently) change it into a pointer, and you'll actually be passing or returning a pointer. That is what is happening here -- the return type gets changed to char *, and you return a pointer to your local array that is going out of scope, so the pointer you end up with is dangling.

CodePudding user response:

There are a couple of issues, mainly on char finalString[count];

  1. finalString is a variable created within the function, it is called a local variable. It would be destroyed after the function returns.

  2. count as the value of the count variable is dynamically changed. Its value could not be determined during the compile stage, thus the compiler could not allocate memory space for this array. The compiling would fail.

To fix this issue.

  1. either create this variable outside and pass it into the function. Or create the variable on the HEAP space. As the Heap space are shared across the entire program and it would not be affected by function ending.

  2. either using a constant number or dynamically allocating a chunk of memory with malloc, calloc and etc.


Here is one demo with the full code:

// main.c
#include <stdio.h>
#include <string.h> 
#include <stdlib.h> 
char* makeString(char character,int count) {
    char* finalString = malloc(count 1);  //dynamic allocate a chunk of space.  1 for mimic string end with an extra termination sign 0.
    for(int i=0; i<count; i  ) {
        finalString[i] = character;
    }
    finalString[count] = 0; // string end signal 0
    return finalString;  // the memory finalString pointed to is in the HEAP space.
}

int main() {
    char * p = makeString('*',5);
    printf("%s\n",p);
    free(p);  // free up the HEAP space
    return 0;
}

  • To compile and run the code.

    gcc -Wall main.c 
    ./a.out
    
  • The output

    *****
    
  • Related