在某些使用 C 等编程语言的软件中是否有间接寻址的实际应用? [英] Is there any real world application of indirect addressing in some software using programming language like C?

查看:52
本文介绍了在某些使用 C 等编程语言的软件中是否有间接寻址的实际应用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在观看本教程,这家伙讲述了间接使用指针寻址.他说可以通过主函数中的函数调用传递地址,可以在函数定义处通过指针变量收集地址.这样我们就可以更改 main 函数中变量的值.这在实际案例中有何用处?如果有人在实际案例软件构建中使用它进行编程,这是否会被滥用.请赐教.任何人都可以给我一个在软件架构中使用间接寻址的真实世界示例吗?

I was watching this tutorial and this guy tells about indirect addressing using pointers. He says that an address can be passed through the function call in main function which can be collected at the function definition by pointer variables. So that we can change the value of variable that are inside main function. How is this useful in real case scenarios? Doesn't this be misused if some one uses this for programming in real case software building. Please enlighten me. Can anyone please give me an example of real world example where indirect addressing is used in software architecture?

main()
{
    int var1;
    function1(int &var1)
}
void function1(int *var1){
    *var1=*var1+1;
}

推荐答案

这在实际案例中有何用处.

How is this useful in real case scenarios.

好吧,即使是最基本的hello world"输入示例也需要有另一个函数(在本例中为 scanf())能够修改 main() 中的变量代码>:

Well, even the most basic "hello world" input example needs to have another function (in this case scanf()) be able to modify a variable inside main():

#include <stdio.h>

int main(void)
{
    char input;
    printf("Enter a single character: ");
    scanf("%c", &input);
    printf("You entered: %c\n", input);
    return 0;
}

如果你不能把input的地址传给scanf(),那么它就不能修改了.

If you couldn't pass the address of input to scanf(), then it wouldn't be able to modify it.

指针是需要的,因为C语言的求值策略是按值调用"(也称为按值传递").这意味着在调用函数时,所有传入的参数都会在之前求值 函数调用发生.这样做的结果是被调用的函数无法访问传递给它的变量.它只能访问该变量的值.

Pointers are needed because the evaluation strategy of the C language is "call by value" (also known as "pass by value".) This means that when calling a function, all parameters that are passed are evaluated before the function call occurs. The effect of this is that the called function has no access to the variable that is being passed to it. It only has access the value of that variable.

通常这很好.并非所有函数都需要访问正在传递的变量.例如printf()不需要访问input,它只需要input的值.

Very often this is fine. Not all functions need to have access to the variable that is being passed. printf() for example doesn't need access to input, it only needs the value of input.

但另一方面,像 scanf() 这样的函数对 input 的值不感兴趣.它需要以某种方式访问​​变量本身.为了实现这一点,需要传递一个指针,该指针求值为一个地址.被调用的函数接收该地址,因此可以访问存储在该地址的任何变量.

But a function like scanf() on the other hand is not interested in the value of input. It needs to somehow access the variable itself. To achieve that, a pointer needs to be passed, which evaluates to an address. The called function receives that address and thus has access to whatever variable is stored at that address.

如果有人在实际案例软件构建中使用它进行编程,这会不会被滥用.

Doesn't this be misused if some one uses this for programming in real case software building.

是的.处理指针的错误是最常见的软件错误来源之一.还有其他语言非常注重避免使用指针,以此来消除与指针相关的软件缺陷.

Yes. Mistakes in handling pointers are among the most popular sources of software bugs. There are other languages out there with a strong focus on avoiding pointers as a means to eliminate pointer-related software defects.

这篇关于在某些使用 C 等编程语言的软件中是否有间接寻址的实际应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆