里面的函数指针操作 [英] Pointer manipulation inside function

查看:110
本文介绍了里面的函数指针操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有指向内存的指针

Let's say I have a pointer pointing to memory

0x10000000

我要添加到它,所以它遍历掉电记忆,例如:

I want to add to it so it traverses down memory, for example:

0x10000000 + 5 = 0x10000005

我将如何做到这一点的一个函数里?我将如何传递的指针指向,并在函数中添加5到它,那么该函数完成后的地址,我可以使用这个值?

How would I do this inside a function? How would I pass in the address that the pointer points to, and inside the function add 5 to it, then after the function's complete, I can use that value?

推荐答案

基本上,就像每个人都如说你要通过引用传递指针(这是一个指向指针的指针又名双指针)的其他功能。

Basically, like what everyone else as said you have to pass the pointer by reference(that is a pointer to a pointer aka double pointer) to the function.

#include<stdio.h>

void IncrementAddress(void **addr)
{
   int offset=5;//give what ever value you want

   *addr = *addr + offset;
    printf("%d\n",*addr);
}  

int main()
{
  char x;// or any other data type like int x or float x etc.
  void *ptr=NULL;
  ptr = &x;

  printf("%d\n",ptr);
  IncrementAddress(&ptr);
  printf("%d\n",ptr);

  return 0;
}

这篇关于里面的函数指针操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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