I want to create a static array of string in a function , then the return value of this function will assign to another array of string .
but it seems I can't do this assignment array2 = function(); but I think use char * for array of string is ok?
what's wrong in my code?
thanks
here is my code
#include <stdio.h>
#include <stdlib.h>
char * function();
int main(){
char * array2[100] = {};
array2 = function();
// print this array of string
int i;
for(i=0;i<strlen(array2);i++){
printf("%s\n",array2[i]);
}
system("pause");
}
char * function(){
static char * array[100] = {};
array[0] = "100";
array[1] = "200";
return array;
}
funtionbefore main but in main, you refer tofunctionchar ** funtion(){and trystatic char **function()just because the data it returns isstatic, which seems to be what you're suggesting. You make a functionstaticif you don't want it to be visible outside the source file (translation unit) in which it is defined. That use ofstaticis unrelated to its use in declaring a variable inside a function. Therestaticmeans that the variable has a lifetime as long as the program, not merely as long as the function call is in effect.const,volatileandrestrict(and_Atomicin C11) are 'type qualifiers'; the keywordsstatic,extern,auto,register(and, surprisingly,typedef, plus_Thread_localadded in C11) are 'storage class specifiers'. They do different jobs in the syntax. In a declarationconst char **function();, you specify that the function takes an indeterminate (not empty!) but fixed (not varargs) argument list and it returns aconst char **.