[英]I don't think I'm using pointers correctly. C
So I've been trying to do this assignment for a few hours and I can't figure it out, I'm 99% sure it has to do with how I used the pointers. 所以我一直试图做这个任务几个小时,我无法弄明白,我99%肯定它与我如何使用指针有关。 And the error I get is a break 0xC0000005: Access violation writing location 0xCCCCCCCC.which I think means some of my pointers are null.
我得到的错误是一个中断0xC0000005:访问冲突写入位置0xCCCCCCCC.which我认为我的一些指针是null。 Sorry for the amateur code in advance..
对不起业余代码提前..
#include<stdio.h>
void load(int *salary)
{
printf("Please enter your salary: ");
scanf("%d", *salary);
}
void calc(int *salary, float *rate, int *raise, int *newsalary)
{
rateofsalary(&salary);
*raise = *salary/(*rate);
*newsalary = *raise+*salary;
}
float rateofsalary(int *salary)
{
float rate;
if(*salary<0 && *salary>=30000)
rate = 7.0;
else
if(*salary<30000 && *salary>=40000)
rate = 5.5;
else
if(*salary<40000)
rate = 4.0;
return rate;
}
void print(int *salary, float *rate, int *raise, int *newsalary)
{
printf("| | Salary | Rate % | Raise | New Salary |\n");
printf("| | %d | %0.2f | %d | %d |\n", salary, rate, raise, newsalary);
}
void main()
{
int salary, raise, newsalary;
float rate;
load(&salary);
rateofsalary(&salary);
calc(&salary, &rate, &raise, &newsalary);
print(&salary, &rate, &raise, &newsalary);
}
for the load function it should be 对于负载功能应该是
void load(int *salary)
{
printf("Please enter your salary: ");
scanf("%d", salary);
}
scanf("%d", *salary);
change it to 改为
scanf("%d", salary);
and the printf
function should be 和
printf
函数应该是
printf("| | %d | %0.2f | %d | %d |\n", *salary, *rate, *raise, *newsalary);
well in this line scanf("%d", *salary);
在这行
scanf("%d", *salary);
instead of *salary
just put salary
so replace it with this scanf("%d", salary);
而不是
*salary
只是把salary
所以用这个scanf("%d", salary);
替换它scanf("%d", salary);
In general, your problem is that you don't know when to quit :) 一般来说,你的问题是你不知道什么时候退出:)
If something is of type int *
, that means it's a pointer to an int
, and it's not necessary nor correct to turn around and use &
on it, which turns it into a pointer to pointer to int. 如果某些东西是
int *
类型的东西,那意味着它是一个指向int
的指针,并且转向并使用&
它不是必需的也不是正确的,这会将它变成指向int的指针。 You make this mistake multiple times, not only in the first short function as other answers have pointed out, but in multiple places. 你多次犯了这个错误,不仅在第一个短函数中,正如其他答案所指出的那样,而是在多个地方。 You manage to turn the variable
salary
from main into a pointer to pointer to pointer to int by the time you're through, adding a level of indirection each time you call another function. 您设法将变量
salary
从main转换为指向指向int的指针的指针,在您每次调用另一个函数时添加间接级别。
Just keep track of what each function wants. 只需跟踪每个功能想要的内容。 If a function accepts an
int*
as an argument, and calls another function that wants an int*
, then it can just pass the variable along as-is: you don't need to add a &
to it. 如果函数接受
int*
作为参数,并调用另一个想要int*
函数,那么它只能按原样传递变量:您不需要向它添加&
。
There are few places you use pointers wrong. 很少有地方使用指针错误。 What others didn't mention yet is: in function:
其他人没有提到的是:功能:
void calc(int *salary, float *rate, int *raise, int *newsalary)
{
rateofsalary(&salary);
*raise = *salary/(*rate);
*newsalary = *raise+*salary;
}
you call rateofsalary
with &salary
, which is pointer to pointer to int
(int **), but formal argument of rateofsalary
is declared as int *
which is pointer to int
. 你用
&salary
调用rateofsalary
,这是pointer to pointer to int
(int **)的pointer to pointer to int
,但是rateofsalary
形式参数被声明为int *
,它是pointer to int
。
It's not the best idea to variable by reference to function, that shouldn't be allowed to change that variable. 通过引用函数进行变量不是最好的想法,不应该允许更改该变量。 Pass it by value or declare function parameter as
const
. 通过值传递或将函数参数声明为
const
。
Whole idea of passing by reference is to be able to modify variable passed to function inside function body and you seem to be missing what pointers actually are or practical use of them. 通过引用传递的整个想法是能够修改传递给函数体内函数的变量,并且您似乎缺少实际使用的指针或实际使用它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.