传递函数指针 [英] passing pointers to functions

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

问题描述

大家好,

我一直在搜寻网络,但是我不太了解如何复制char数组并将值传递给另一个函数.

假设我有3个阵列;

字符buf1 [50],buf2 [50],buf3 [50]

我正在尝试将每个缓冲区中的数据复制并将其传递给另一个函数,但是所有有关指针的讨论使我一头雾水,甚至功能"一词对我也失去了所有的意义!

Hi Guys,

I''ve been scouring the net but I can''t quite get my head around how to copy a char array and pass the values to another function.

Say I have 3 arrays;

char buf1[50], buf2[50], buf3[50]

I am trying to copy and pass the data within each buffer to another function, but all this talk of pointers has got my head in a spin and even the word "function" has lost all meaning to me!

Would really appreciate the help.

推荐答案

要将一个缓冲区复制到另一个缓冲区,可以执行此操作;

To copy one buffer to another, you can do this;

for(int i=0;i<50;i++)
    buf2[i] = buf1[i];



要将缓冲区传递给函数,可以使用此方法;



To pass the buffer to a function you can use this;

void myfunction(char *buffer)
{
    // do something with buffer here, f.e. set all elements to 0
    for(int i=0;i<;50;i++)
        buffer[i] = 0;

}

void main(void)
{
    char buf1[50], buf2[50], buf3[50];

    // initialise all elements of each buffer to 0, using myfunction()
    myfunction(buf1);
    myfunction(buf2);
    myfunction(buf3);
}


当然在某个地方有一个很好的解释,但是我会写我自己的.

在C/C ++(以及几乎所有其他语言)中,数组只是指向数组中第一个元素的指针.
在C/C ++中,它还会在堆栈上分配内存,而不是使用new分配内存,但这对它们的工作方式并不是很重要.

因此,我们有3个数组(指针)buf1buf2buf3.
这些示例只是指向内存的指针,对于此示例:
buf1=0x1000
buf2=0x1100
buf3=0x1200

然后,从0x1000开始的内存内容就是数组的元素,例如,您具有以下代码:
Surely there is a good explanation somewhere, but I will write my own.

In C/C++ (and pretty much every other language) an array is just a pointer to the first element in the array.
In C/C++ it also allocates memory on the stack, rather than you allocating memory with new, but that isn''t really important to how they work.

So, we have 3 arrays (pointers) buf1, buf2 and buf3.
Each of these is just a pointer to a peice of memory, for this example:
buf1=0x1000
buf2=0x1100
buf3=0x1200

Then the contents of memory starting at 0x1000 is the elements of the array, say for example you have the code:
char buf1[50] = "Hello";
char buf2[50] = "World";
char buf3[50] = "Array";


然后,内存将如下所示:


Then the memory would look like this:

Offset 0x1000:
''H'', ''e'', ''l'', ''l'', ''o'', ''\0'', (rest of 50 chars in undefined)
Offset 0x1100:
''W'', ''o'', ''r'', ''l'', ''d'', ''\0'', (rest of 50 chars in undefined)
Offset 0x1200:
''A'', ''r'', ''r'', ''a'', ''y'', ''\0'', (rest of 50 chars in undefined)


每个字符是1个字节.

当您将此指针传递给函数时,它仅传递内存位置的副本,例如:


Where each character is 1 byte.

When you pass this pointer into a function, it just passes a copy of the memory location, for example:

void Print(char *str) {
	printf(str); //print variable <str> to the screen. <str> points to a bit of memory that has a string.
}

int main() {
	char buf1[50] = "Hello"; //Memory location 0x1000
	char buf2[50] = "World"; //Memory location 0x1100
	char buf3[50] = "Array"; //Memory location 0x1200
	Print(buf1); //This calls Print(0x1000)
}



然后,将指向buf1的指针传递到Print(),这是内存位置0x1000.
这不会创建内存的副本,因此,如果函数Print()更改了内存中的任何内容,它将影响Print()main()中的字符串.
如果Print()中的任何内容均未更改,则在此示例中没有任何更改,则无需复制内存.
如果更改了某些内容,并且希望此更改返回到调用函数main()(例如转换为大写字母),则不得复制内存(除非您也希望以原始大小写进行复制). > 如果Print()中的某些内容发生了更改,并且您不想让它更改main()中的数组,那么您需要复制该数组,可以使用另一个缓冲区或某些已分配的内存来完成:



Then what happens is the pointer to buf1 is passed to Print(), which is the memory location 0x1000.
This does not create a copy of the memory, and so if the function Print() changes anything in the memory, it will affect the string in both Print() and main().

If nothing is changed in Print(), in this example there isn''t, then there is no need to copy the memory.
If you change something and you want this change to go back to the calling function main() (such as convert to upper case) you MUST NOT copy the memory (unless you want a copy in the original case as well).
If something is changed in Print() and you DON''T want this to change the array in main() then you need to copy the array, this can be done with another buffer or some allocated memory:

char buf1[50] = "Hello";
char buf1Copy[50];
strcpy(buf1Copy, buf1); //Copies the string (5 characters + 1 null terminating character ''\0''). Only works for strings.
/* OR... */
memcpy(buf1Copy, buf1, sizeof(buf1)); //Copies all 50 bytes of buf1 into buf1Copy. Works for any array.


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

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