我想學習c主函數調用一些其他函數返回一個數組(例如字符串類型)。不兼容指針整數轉換錯誤
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* array_return(char* name);
int main(){
char* name="orange";
char* A;
A=array_return(name);
free(A);
printf("%s\n","Check stored values in A");
int k,n;
n=10;
for (k=0; k < n; k++) {
printf("%s\n",A[k]);
}
return 0;
}
char* array_return(char* name){
int k,n;
n=10;
char* A=malloc(n);
char string[100];
printf("%s\n","Store values in A");
for (k=0; k < n; k++) {
strcpy(string,"Numer of ");
strcat(string,name);
strcat(string," are: ");
sprintf(string,"%s %i",string,k);
A[k]=string;
printf("%s\n",A[k]);
}
printf("%s\n","--------------------------");
return A;
}
我試圖商店按照陣列 「A」 值:
Numer of orange are: 0
Numer of orange are: 1
Numer of orange are: 2
Numer of orange are: 3
Numer of orange are: 4
Numer of orange are: 5
Numer of orange are: 6
Numer of orange are: 7
Numer of orange are: 8
Numer of orange are: 9
不過,我得到以下一些警告
warning: format specifies type 'char *' but the argument has type
'char'
incompatible pointer to integer conversion assigning to 'char'
from 'char [100]' [-Wint-conversion]
而且,我得到以下錯誤,當我運行程序:
Segmentation fault: 11
如果有人可以幫助我調試此代碼,我將非常感激。
您無法將數組傳遞給函數或從函數傳遞數組。但你可以傳遞一個指針。閱讀[問]並使用調試器。 – Olaf
你的代碼有很多問題。 – BLUEPIXY
「*例如字符串類型*」在C中沒有類型「字符串」 – alk