我很抱歉重复这个问题,但我无法理解(我认为我的问题是什么)指针。有人可以为我介绍一下吗?我已经搜索了大量的stackoverflow问题,c tutoriols和它的混乱,因为每个解决方案似乎都与另一个解决方案相矛盾 - 如果我看到我的代码被修复,我肯定会帮助我完全理解
1 #include <stdio.h>
2 getinput()
3 {
4 int *employeeSalary[5];
5 int i;
6 for(i=0;i<5;i++)
7 {
8 printf("Enter details of employee no. %d :",i+1);
9 scanf("%d",&employeeSalary[i]);
10 }
11 return(*employeeSalary);
12 }
13
14 int payrise(int *employeeSalary[])
15 {
16 int *newSalary = ((employeeSalary*0.20)*100);
17 return(*newSalary);
18 }
19
20 int highestSalary(int *newSalary)
21 {
22 int i = 0;
23 int biggestSalary;
24 int element;
25 if(newSalary[i] >= newSalary[i])
26 {
27 biggestSalary = newSalary[i];
28 element = i;
29 }
30 return(biggestSalary, element);
31 }
32
33 void display(int *employeeSalary, int *newSalary, int biggestSalary, int element)
34 {
35 printf("The old salary was %d and the new one is %d ", employeeSalary, newSalary);
36 printf("The biggest salaray is %d in element %d", biggestSalary, element);
37 }
38
39
40 int main()
41 {
42 int initialSalary = getinput();
43 int payIncrease = payrise(initialSalary);
44 int largestSalary = highestSalary(payIncrease);
45 display(initialSalary, payIncrease, largestSalary);
46 return(0);
47 }
这些是我得到的错误
11: error: invalid type argument of ‘unary *’ (have ‘int’)
16: error: invalid operands to binary * (have ‘int ***’ and ‘double’)
17: error: invalid type argument of ‘unary *’ (have ‘int’)
45: error: too few arguments to function ‘display’
答案 0 :(得分:0)
在第2行,你看起来像没有返回类型的函数声明,尝试添加一个。
答案 1 :(得分:0)
这主要是打字问题
@lijat说的是对的。在第16行,你有一个没有不同类型的演员的作业。
((employeeSalary*0.20)*100)
的类型为int [],实际上是int * ,所以你需要让左侧为int **或者再次引用右侧(在*处添加*)表达的开头。
返回后,第17行需要一个空格。
第45行你缺少一个参数,功能显示接收4,你给它只有3。
答案 2 :(得分:0)
你使用的指针都错了。在尝试使用它们之前,请阅读指针。我会试着指出你的错误:
2 getinput() //<-- no return type? should return int*
3 {
4 int *employeeSalary[5];
5 int i;
6 for(i=0;i<5;i++)
7 {
8 printf("Enter details of employee no. %d :",i+1);
9 scanf("%d",&employeeSalary[i]);
10 }
11 return(*employeeSalary); //<-- I guess you wanted to return the whole array. This only returns the 1st element
12 }
14 int payrise(int *employeeSalary[])//<-- must return an integer
15 {
16 int *newSalary = ((employeeSalary*0.20)*100); <-- employeeSalary is an array of int pointers, therefore this is invalid
17 return(*newSalary);
18 }
20 int highestSalary(int *newSalary) //<-- must return an integer like 5 or 3 ...
21 {
22 int i = 0;
23 int biggestSalary;
24 int element;
25 if(newSalary[i] >= newSalary[i]) //<-- isn't this always true, i.e. always equal?
26 {
27 biggestSalary = newSalary[i];
28 element = i;
29 }
30 return(biggestSalary, element); //<-- no comment! I don't know what you are trying to do here
31 }
void display(int *employeeSalary, int *newSalary, int biggestSalary, int element)
34 {
35 printf("The old salary was %d and the new one is %d ", employeeSalary, newSalary);//employeeSlary and newSalary are pointersn you need to dereference them.
36 printf("The biggest salaray is %d in element %d", biggestSalary, element);
37 }
我强烈建议您在线查找c教程并遵循它。看来你无法区分变量类型和指针。
答案 3 :(得分:-4)
#include <stdio.h>
#include <stdlib.h>
#define NUM_OF_EMPLOYEE 5
int *getinput(void){
int i, *employeeSalary = calloc(NUM_OF_EMPLOYEE, sizeof(int));
for(i=0;i<NUM_OF_EMPLOYEE;++i){
printf("Enter details of employee no. %d :", i+1);
scanf("%d", &employeeSalary[i]);
}
return employeeSalary;
}
int *payrise(int employeeSalary[]){
int i, *newSalary = malloc(NUM_OF_EMPLOYEE * sizeof(int));
for(i=0;i<NUM_OF_EMPLOYEE;++i){
newSalary[i] = employeeSalary[i]*1.20;//*0.20*100 = *20.0
}
return newSalary;
}
int highestSalary(int *newSalary){
int biggestSalary = newSalary[0];
int element = 0;
int i = 0;
for(i=1;i<NUM_OF_EMPLOYEE;++i){
if(newSalary[i] > biggestSalary){
biggestSalary = newSalary[i];
element = i;
}
}
return element;
}
void display(int *employeeSalary, int *newSalary, int biggestSalary, int element){
int i;
for(i=0;i<NUM_OF_EMPLOYEE;++i){
printf("The old salary was %d and the new one is %d\n", employeeSalary[i], newSalary[i]);
}
printf("The biggest salaray is %d in element %d\n", biggestSalary, element);
}
int main(){
int *initialSalary = getinput();
int *payIncrease = payrise(initialSalary);
int index = highestSalary(payIncrease);
int largestSalary = payIncrease[index];
display(initialSalary, payIncrease, largestSalary, index);
free(initialSalary);free(payIncrease);
return 0;
}