Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于返回自定义长度字符的C函数_C_String_Function_For Loop - Fatal编程技术网

用于返回自定义长度字符的C函数

用于返回自定义长度字符的C函数,c,string,function,for-loop,C,String,Function,For Loop,请注意,我是C语言的新手。我正在编写一个函数,该函数接收一个数字并返回一个由接收长度的'*'组成的*字符。i、 e: createHiddenName(6)//returns "******" createHiddenName(4)//returns "****" 我已经这样编码了,但它不起作用: char *createHiddenName(int length) { char *hidden[length];// int i; for (i = 0; i <

请注意,我是C语言的新手。我正在编写一个函数,该函数接收一个数字并返回一个由接收长度的
'*'
组成的
*字符。i、 e:

createHiddenName(6)//returns "******"
createHiddenName(4)//returns "****"
我已经这样编码了,但它不起作用:

char *createHiddenName(int length)
{
    char *hidden[length];//
    int i;
    for (i = 0; i < length; i++) {
        hidden[i] = '*';
    }
    return *hidden;
}
char*createHiddenName(int-length)
{
字符*隐藏[长度]//
int i;
对于(i=0;i

任何帮助都将不胜感激。非常感谢您

在您最初的方法中

   char *hidden[length]; // Why would you like to have an array of pointers?
   return *hidden; // Wrong because unless 'malloc'ated, a pointer inside the function will not work after the return, Consider what happens if the function stack is cleared.
相反,您可以遵循以下方法

#include<stdio.h>
#include<string.h>
char hidden_name[100]; 
// global char array for storing the value returned from function
char *createHiddenName(int length)
{
            char temp[length+1];
            int i;
            for (i = 0; i < length; i++) {
                        temp[i] = '*';
                        }
                temp[i]='\0'; // Null terminating  temp
            strncpy(hidden_name,temp,(size_t)(length+1));
            //Remember temp perishes after function, so copy temp to hidden_name
            return hidden_name;
}


int main(){
printf("Hidden Name : %s\n",createHiddenName(6));
return 0;
}
#包括
#包括
字符隐藏名称[100];
//用于存储函数返回值的全局字符数组
char*createHiddenName(整数长度)
{
字符温度[长度+1];
int i;
对于(i=0;i
两个主要问题:

char *hidden[length];
这将
hidden
定义为指向
char
的指针数组。它可以是字符串数组,而不是字符串本身

然后尝试返回指向该数组的指针,但该数组是一个超出范围的局部变量,一旦函数返回,它将不再存在。使用返回的指针将导致未定义的行为

最简单的解决方案是将要填充的缓冲区作为参数传递。差不多

char *createHiddenName(int length, char *hidden)
{
    ...
    return hidden;
}

当然,请记住创建一个足够大的缓冲区,以容纳包括空终止符(现在不添加)在内的完整字符串。

您需要使用动态内存分配,如下所示

char *createHiddenName(int length)
{
  char *hidden = malloc((length+1) * sizeof(char));
  if(hidden == NULL) {
    return NULL;
  }
  int i;
  for (i = 0; i < length; i++) {
    hidden[i] = '*';
  }
  hidden[i] = '\0'; //Null terminated string
  return hidden;
}

返回*隐藏…什么?为什么?不要返回任何东西,传递一个缓冲区来填充。编译时所有警告都打开。认真对待编译器的警告。修复代码,直到不再发出警告。不要盲目地“抛弃”警告。买一本好的入门书。从反复试验中学习C语言可能会导致灾难。那些拥有所有链接的人在哪里?@DeiDei-非常感谢。
sizeof(char)
就是其中之一。
char *ptr = createHiddenName(10);
//....
// Use ptr
//....
// done ? then free it 
free(ptr);
ptr = NULL;