我必须编写一个程序,它具有一个函数,该函数对数组中的所有正数进行求和,但使用函数参数数组作为指针.当我尝试调用sum函数时,在main函数中出现问题.这是我的源代码:
#include <stdio.h>
#include <conio.h>
int posit(int *x[], int n){
int s=0;
for(int i=0; i<n; i++){
if(*x[i]>0){
s=s+*x[i];
}
}
return s;
}
int main(){
int k;
scanf("%d",&k);
int a[k];
for(int i=0; i<k; i++){
scanf("%d",&a[i]);
}
int b=posit(&a,k);
printf("%d\n", b);
getch();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 这是一个应该找到字符串中最常见元素的程序.但是当我输入一个字符串时它会崩溃.
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main(){
char a[100];
scanf("%s", a);
int max=0,n,k;
int urt = strlen(a);
for(int i=0; i<urt-1; i++){
n=0;
for(int l=i+1; l<urt; l++){
if(a[i]==a[l]) n++;
}
if(max<n){
max=n;
k=i;
}
}
printf("%s\n", a[k]);
printf("%d", max);
getch();
return 0;
}
Run Code Online (Sandbox Code Playgroud)