简体   繁体   English

我不认为我正确使用指针。 C

[英]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.

相关问题 带有指针的C结构认为我没有正确分配内存 - C struct with pointer think I'm not allocating memory correctly 有人可以请问为什么这个程序不能正常运行。 我是C新手 - Could someone please why this program is not running correctly. I'm new to C 我不懂 C 指针 - I don't understand C pointers 我不知道我做错了什么。 我认为我的条件是正确的 - I don't know what I'm doing wrong. I think my condition is right 我想在来自 void 函数的结构数组中添加信息,但我认为我没有正确使用指针 - I want to add info in a array of structures from a void function but i think i don't use the pointers right 在C编程中,我试图使用指针反向打印数组,但似乎无法获取它 - In C programming, I'm trying to print my array in reverse using pointers but I can't seem to get it 我不明白指针 - I don't understand pointers 在 C 中,我正在为指针而苦苦挣扎 - In C, I'm struggling with Pointers 我不明白这个用指针编写的 C 程序 - I don´t understand this C program made with pointers 使用带有结构变量的指针的递归函数中的语法。 它不起作用,我认为主要问题是语法 - Syntax in recursive functions using pointers with struct variables. It doesn't work, and I think the main problem is the syntax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM