十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这问题说明你对指针的运用还是不熟练的,既然是数组,作为函数参数传递的是数组首地址.
成都创新互联专业为企业提供荣昌网站建设、荣昌做网站、荣昌网站设计、荣昌网站制作等企业网站建设、网页设计与制作、荣昌企业网站模板建站服务,十年荣昌做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
#include stdio.h
#include stdlib.h
void func1(int *array, int size) {
int i,temp;
for (i=0;isize;i++) {
scanf("%d",temp); //利用scanf输入赋值
array[i]=temp;
}
}
//数组array_b根据数组array_a的元素乘以项数获得元素值
void func2(int *array_a,int *array_b, int size) {
int i;
for (i=0;isize;i++)
array_b[i]=array_a[i] * i;
}
//输出数组元素
void print(int *array, int size) {
int i;
for (i=0;isize;i++)
printf("%d ",array[i]);
printf("\n");
}
int main(void) {
int size; //数组大小
int i;
int *array_a, *array_b; //声明数组array_a,array_b
scanf("%d", size);
array_a=malloc(sizeof(int) * size);
array_b=malloc(sizeof(int) * size);
func1(array_a, size);
func2(array_a, array_b, size);
print(array_a,size);
print(array_b,size);
return 0;
}
#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并将指针移动到字符串的末尾的下一个位置*/
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组字符串:");
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组字符串:");
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;
}
运行结果
//考点是数组作为参数入参,第一个参数为数组指针,第二个参数为数组长度,第三个参数为被成书
void func(int a[], int size, int x){
for(int i = 0; i size; i++){
a[i] *= x;
printf("%d ", a[i]);
}
printf("\n");
}
int main(){
int a[4], x;
scanf("%d,%d,%d,%d", a[0], a[1], a[2], a[3]);
scanf("%d", x);
func(a, 4, x);
return 0;
}
程序没问题,你想问什么,是什么地方不懂吗,不懂的地方标出来!
给你看一下我的代码
#includestdio.h
void main(){
int a[10],b[10],i,j,temp;
for(i=0;i10;i++){
printf("请输入第%d个数:",(i+1));
scanf("%d",a[i]);
//保存数组,因为输入完数组要冒泡排序,排序后的元素顺序可能和原数组不一样
b[i]=a[i];
}
//冒泡排序,求最大值
for(i=0;i9;i++){
for(j=0;j9-i;j++){
if(a[j]a[j+1]){ //这里控制的升序还是降序,我这是降序
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
//查找最大值在原数组的位置
for(i=0;i10;i++){
if(b[i]==a[0]){ //降序的话最大值是a[0],升序的话最大值是a[9]
break; //一旦找到最大值,马上跳出循环
}
}
printf("最大值是%d,它在原数组中是第%d个元素!\n",a[0],(i+1));
}