c语言数组的几种函数 c语言数组的几种函数是什么

c语言,字符数组函数#include stdio.h
/*比较两个字符串*/
/*src、dst相等 return 1*/
/*src比dst短 return -2*/
/*src比dst长 return 2*/
/*src、dst长度一致但不相等 return -1*/
int stringCmp (char *src, char *dst) {
if (strlen (src)strlen (dst))
return -2;
else if (strlen (src)strlen (dst))
return 2;
else {
while (*src*dst) {
if (*src != *dst)
return -1;
src++;
dst++;
}
return 1;
}
}
/*合并两个字符串*/
void mergeString(char* str1, char* str2, char* merge) {
while (*merge++ = *str1++); /*复制str1并将指针移动到字符串c语言数组的几种函数的末尾c语言数组的几种函数的下一个位置*/
merge--; /*退到字符串的末尾*/
while (*merge++ = *str2++); /*从末尾(\0)的位置开始将str2拷贝进来*/
*merge = '\0';
}
int main(void) {
char *str1_1="hel",*str1_2="lo", *merge1;
char *str2_1="ness",*str2_2="happy", *merge2;
char *str3_1="hi",*str3_2="hi", *merge3;
char *str4_1="hello",*str4_2="world", *merge4;
puts ("第1组字符串c语言数组的几种函数:");
puts (str1_1);
puts (str1_2);
if (stringCmp (str1_1, str1_2) == 1)
puts (str1_1);
else if (stringCmp (str1_1, str1_2) == -2) {
mergeString (str1_2, str1_1, merge1);
puts (merge1);
}
else if (stringCmp (str1_1, str1_2) == 2 || stringCmp (str1_1, str1_2) == -1) {
mergeString (str1_1, str1_2, merge1);
puts (merge1);
}
putchar ('\n');
puts ("======================================================");
putchar ('\n');
puts ("第2组字符串c语言数组的几种函数:");
puts (str2_1);
puts (str2_2);
if (stringCmp (str2_1, str2_2) == 1)
puts (str2_1);
else if (stringCmp (str2_1, str2_2) == -2) {
mergeString (str2_2, str2_1, merge2);
puts (merge2);
}
else if (stringCmp (str2_1, str2_2) == 2 || stringCmp (str2_1, str2_2) == -1) {
mergeString (str2_1, str2_2, merge2);
puts (merge2);
}
putchar ('\n');
puts ("======================================================");
putchar ('\n');
puts ("第3组字符串:");
puts (str3_1);
puts (str3_2);
if (stringCmp (str3_1, str3_2) == 1)
puts (str3_1);
else if (stringCmp (str3_1, str3_2) == -2) {
mergeString (str3_2, str3_1, merge3);
puts (merge3);
}
else if (stringCmp (str3_1, str3_2) == 2 || stringCmp (str3_1, str3_2) == -1) {
mergeString (str3_1, str3_2, merge3);
puts (merge3);
}
putchar ('\n');
puts ("======================================================");
putchar ('\n');
puts ("第4组字符串:");
puts (str4_1);
puts (str4_2);
if (stringCmp (str4_1, str4_2) == 1)
puts (str4_1);
else if (stringCmp (str4_1, str4_2) == -2) {
mergeString (str4_2, str4_1, merge4);
puts (merge4);
}
else if (stringCmp (str4_1, str4_2) == 2 || stringCmp (str4_1, str4_2) == -1) {
mergeString (str4_1, str4_2, merge4);
puts (merge4);
}
putchar ('\n');
puts ("======================================================");
putchar ('\n');
getch (); /*屏幕暂留*/
return 0;
}
运行结果
C语言数组与函数你的程序:
main()
{
int b[3];
b=pop();
}
int * pop()
{
int a[3],i;//定义的局部变量a[3]在调用完之后自动释放其空间
for(i=0;i3;i++)
a[i]=i;
return a;//返回的是指向数组a[3]的地址而不是数组a[3]本身
}
注:既然上面说到调用的方法返回的是指向数组a[3]的地址,所以主函数中b接收的应该是指向数组a[3]的地址 , 而数组a[3]在方法调用完后又被释放了,所以b接收不到数组a[3],故出错 。

推荐阅读