我正在学习C的基础知识和原理。
我现在来到指针,字符串和结构。
现在我正在使用此代码来传递数组'内容到功能。
我有这个代码来传递不同数组的内容。
我成功完成的是:
我现在遇到的问题:
到目前为止,这是我的代码:
void print_array(char *arr,int8_t cnt);
void print_array(char *arr,int8_t cnt)
{
int i;
printf("Number of elements is: %d\n",cnt);
for (i=0;i<cnt;i++)
{
printf("Elements of array: %s\n",arr);
}
}
void print_len (char *arr,int8_t cnt);
void print_len (char *arr,int8_t cnt)
{
char i,l;
for (i=0;i<cnt;i++)
{
printf ("%d\n",strlen(arr));
}
}
int main(){
char array_1 [] = {1,2,3,4,5,6,7,8};
char array_2 [] = {'1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G'};
char *array_3 [] = {"1st","2nd","3rd","4th","5th","6th"};
char *array_4 [] = {"Many of the designations used by manufacturers"};
char *array_5 [] = {"mm","End of Multiple Strings Array","simple bluetooth connection",
"datalogging purposes and accessing recorded data","THE OPERATING ENVIRONMENT"};
//int8_t *array_pointer[3]=(char*){&array_1,&array_2,&array_3};
int8_t cnt1 = sizeof(array_1)/sizeof(array_1[0]);
int8_t cnt2 = sizeof(array_2)/sizeof(array_2[0]);
int8_t cnt3 = sizeof(array_3)/sizeof(array_3[0]);
int8_t cnt4 = sizeof(array_4)/sizeof(array_4[0]);
int8_t cnt5 = sizeof(array_5)/sizeof(array_5[0]);
int8_t len1,len2,len3,len4,len5,i,t=0,x=0;
//print_len(*array_3,cnt3);
print_len(*array_5,cnt5);
//printf("Number of chars int the string#%d is: %d\n",i,t);
// this for testing strlen inside main
// I want to process this function outside main
/*for (i=0;i<cnt5;i++)
{
printf ("%d\n",strlen(array_5[i]));
}*/
//print_array(array_pointer[0],cnt1);
//print_array(array_1,cnt1);
//print_array(array_2,cnt2);
//print_array(*array_3,cnt3);
//print_array(*array_4,cnt4);
print_array(*array_5,cnt5);
return 0;
}
答案 0 :(得分:3)
- 如何将多个字符串的数组传递给函数。
醇>
你需要另一个功能。声明为:
void print_array_2(char *arr[], int cnt);
然后,您可以使用:
print_array_2(array_3, cnt3);
- 如何指定指向数组的指针以将它们传递给函数。
醇>
您可以使用:
char* string_array[2] = {};
string_array[0] = array_1;
string_array[2] = array_2;
print_array_2(string_array, 2);