在你原來的做法,你有,
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;
}
'請注意,我是C'.... uummmm..why的新手? –
'char * hidden [length];'...你不需要那個 –
'return * hidden;'......什麼?爲什麼? –