传递和返回函数的数组 [英] Passing and returning arrays to and from functions

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

问题描述

有人可以请求帮助,我正在尝试将数组传递给函数,对该数组执行一些

操作,然后将其返回以供进一步使用。对于以下代码,我得到的错误是:b / b
间接层次的差异,所以

我觉得它必须与我代表数组的方式有关

来电和退货。


下面我评论了问题部分。


提前致谢提供任何帮助。

Pete


#include< stdio.h>

#include< string.h>


char P10(char key []); / *原型* /


void main()

{


char key [20];

int i;


printf(\ n输入10位二进制Sting:);

得到(键) ; / *复制10位二进制字符串的表示

从键盘输入到key []。 不用于二元操作

只是表示* /


/ *以下我收到错误''char []''不同在间接的水平上

''char''

和''=''左操作数必须是l值* /


key = P10(key); / *函数调用。将返回的数组复制到key [] * /

printf(" Key is now:\ n");

for(i = 0; i< 10; + + i){/ *循环打印返回的数组键[] * /

printf("%c\ n",key [i]);

}

}

char P10(char Ctext []){/ *用于置换输入字符串的简单函数* /


int Perm10 [10] = {3,5,2,7,4,10,1,9,8,6};

int i;

int index;


/ *跟踪语句检查键[]已通过* /

for(i = 0; i< 10; ++ i){

printf(" Ctext =%c\ n",Ctext [i]);

}

/ * Trivial Permutation function * /

for(i = 0; i< 10; i ++){

index = Perm10 [i];

printf(" index =%d \\ \\ n",index);

Ctext [i] = Perm10 [index-1];

}

/ *下面显示错误''return'':''char''与''char *''* /


返回Ctext;

间接水平不同/ *将Ctext [] / key []返回给调用函数* /

}

Can someone please help, I''m trying to pass an array to a function, do some
operation on that array, then return it for further use. The errors I am
getting for the following code are, differences in levels of indirection, so
I feel it must have something to do with the way I am representing the array
in the call and the return.

Below I have commented the problem parts.

Thanks in advance for any help offered.
Pete

#include <stdio.h>
#include <string.h>

char P10(char key[]); /* prototype */

void main()
{

char key[20];
int i;

printf("\nEnter a 10 bit Binary Sting: ");
gets(key); /* Copy representation of 10 bit binary string
entered from keyboard to key[]. "not to use for binary operations
just representation */

/* below I''m getting an error ''char[]'' differs in levels of indirection to
''char''
and ''='' left operand must be l-value */

key = P10(key); /* function call. copy returned array into key[] */
printf("Key is now : \n");
for(i=0; i<10; ++i) { /* loop to print returned array key[] */
printf("%c\n", key[i]);
}
}
char P10(char Ctext[]) { /* Trivial function to permute input string */

int Perm10[10]={3,5,2,7,4,10,1,9,8,6};
int i;
int index;

/* Trace statement checking key[] has been passed */
for(i=0;i<10;++i) {
printf("Ctext = %c\n", Ctext[i]);
}
/* Trivial Permutation function */
for(i=0; i<10; i++) {
index = Perm10[i];
printf("index = %d\n", index);
Ctext[i] = Perm10[index-1];
}
/* Below shows the error ''return'' : ''char'' differs in
levels of indirection from ''char *'' */

return Ctext; /* return Ctext[]/key[] to calling function */
}

推荐答案



" Pete" <否**** @ blank.com>写了

"Pete" <No****@blank.com> wrote
#include< stdio.h>
#include< string.h>

char P10(char key []); / * prototype * /

这是正确的,但它表明存在误解。
void main()

这应该是int main()符合ANSI标准。
{char key [20];
int i;

printf(" \\\
输入10位Binary Sting:) ;获得(关键); / *从键盘到键[]输入的10位二进制字符串的复制表示。 不要用于二进制操作
只是表示* /

gets()对于测试程序是可以的,但对于真正的代码则不行,因为你将

如果有人输入超过19个字符(19 + NUL)
/ *以下我会收到错误''char []''间接级别与
''char不同''
和''=''左操作数必须是l值* /

key = P10(key); / *函数调用。将返回的数组复制到键[] * /

这就是误解的地方。
printf(现在是键:\ n);
for(i = 0; i< 10; ++ i){/ *循环打印返回的数组键[] * /
printf("%c\ n",key [i]);
}
}

char P10(char Ctext []){/ *用于置换输入字符串的简单函数* /

int Perm10 [10] = {3,5 ,2,7,4,10,1,9,8,6};
int i;
int index;

/ *跟踪语句检查键[]已被传递* /
for(i = 0; i< 10; ++ i){
printf(" Ctext =%c\ n",Ctext [i]);
}
/ *普通排列函数* /
for(i = 0; i< 10; i ++){
index = Perm10 [i];
printf(" index =% d \ n",index);
Ctext [i] = Perm10 [index-1];

}
/ *下面显示错误''return'': ''char''的间接水平与''char *''* /

返回Ctext不同; / *将Ctext [] / key []返回到调用函数* /
}
#include <stdio.h>
#include <string.h>

char P10(char key[]); /* prototype */
This is right but it suggests misunderstanding.
void main()
This should be int main() to be ANSI-compliant.
{

char key[20];
int i;

printf("\nEnter a 10 bit Binary Sting: ");
gets(key); /* Copy representation of 10 bit binary string
entered from keyboard to key[]. "not to use for binary operations
just representation */
gets() is OK for a test program, but not for real code, because you will
corrupt memory if someone type in more than 19 characters (19 + NUL)
/* below I''m getting an error ''char[]'' differs in levels of indirection to
''char''
and ''='' left operand must be l-value */

key = P10(key); /* function call. copy returned array into key[] */
This is where the misunderstanding creeps in.
printf("Key is now : \n");
for(i=0; i<10; ++i) { /* loop to print returned array key[] */
printf("%c\n", key[i]);
}
}
char P10(char Ctext[]) { /* Trivial function to permute input string */

int Perm10[10]={3,5,2,7,4,10,1,9,8,6};
int i;
int index;

/* Trace statement checking key[] has been passed */
for(i=0;i<10;++i) {
printf("Ctext = %c\n", Ctext[i]);
}
/* Trivial Permutation function */
for(i=0; i<10; i++) {
index = Perm10[i];
printf("index = %d\n", index);
Ctext[i] = Perm10[index-1];
}
/* Below shows the error ''return'' : ''char'' differs in
levels of indirection from ''char *'' */

return Ctext; /* return Ctext[]/key[] to calling function */
}




第一点是大多数C引物引入了多维数组

与一维数组大致相同。这是IMO的一个错误。在C,

一维数组是基本的,但是多维数组是高级的

功能。


这是什么与你有关,因为你只使用1维

数组?


答案是引子掩盖了C不通过的事实或

将数组返回给函数。它传递了

数组的第一个元素的地址。


因此你的函数应该是


char P10(char * Ctext)


但是几乎所有的程序员都希望得到一些关于数组长度的安全性(而不仅仅是依赖于调用者读取的数据)评论和

从名字中猜出P10需要10个字符)


所以原型变成


/ *

P2 - 置换数组

Ctext - 要置换的文本

N - 文本长度(当前版本必须为10)

* /

char P10(char * Ctext,int N)


现在我们传递指向数组的指针,数组将被置换

到位。因此无需返回任何东西。所以函数变成了


void P2(char * Ctext,int N)


然而,让我们说你不想修改数组通过了。我们做什么

呢?答案是


void P2(const char * Ctext,int N,char *输出)


如果你打算允许Ctext和输出相同,你需要仔细考虑
代码。



The first point is that most C primers introduce multi-dimensional arrays at
about the same time as one-dimensional arrays. This is IMO a mistake. In C,
one-dimensional arrays are basic, but multi-dimensional arrays are advanced
features.

What does this have to do with you, since you only use a 1 dimensional
array?

The answer is that the primer obscures the truth that C does not pass or
return arrays to functions. It passes the address of the first element of
the array.

Your function should therefore be

char P10(char *Ctext)

However virtually any programmer would want some security about the length
of the array (and not just rely on the caller reading the comments and
guessing from the name that P10 takes 10 characters)

So the prototype becomes

/*
P2 - permute an array
Ctext - text to permute
N - length of text ( must be 10 in current version)
*/
char P10(char *Ctext, int N)

Now since we are passing a pointer to the array, the array will be permuted
in place. There is thus no need to return anything. So the function becomes

void P2(char *Ctext, int N)

However let''s say you do not want to modify the array passed. What do we do
then? The answer is

void P2(const char *Ctext, int N, char *output)

If you are going to allow Ctext and output to be the same, you will have to
code carefully.


2004年9月17日星期五12:08:12 +1000, "皮特" <否**** @ blank.com>写在

comp.lang.c:
On Fri, 17 Sep 2004 12:08:12 +1000, "Pete" <No****@blank.com> wrote in
comp.lang.c:
有人可以帮助,我正在尝试将一个数组传递给一个函数,做一些我觉得它必须与我在调用中表示数组的方式有关。回报。


那是因为你无法通过或返回

函数传递裸阵列。

下面我已经评论了问题部分。

提前感谢您提供的任何帮助。
Pete

#include< stdio.h>
#include< ; string.h>

char P10(char key []); / *原型* /


这个原型完全等同于:


char P10(char * key);


....因为当用作函数的参数时,数组的名称总是转换为指向

的指针。 C允许

您用作我使用的符号的同义词。我真的希望从未被允许过,因为它只会增加许多新手所拥有的数据/指针混乱。


即使你可以从一个函数返回一个数组,你不能用b
$ b,你已经创建了一个返回类型为单一的函数

char只有。


实际上,你想把这个函数的返回类型改为void,

在函数本身看到我的评论。

void main()


这是你忽略评论的问题部分。唯一的

标准,托管C环境中main()的可移植返回类型是

''int''。无论一些编译器的文盲帮助文件是什么

说。


int main()


.. ..或者更好:


int main(无效)

{

char key [20];
int i;

printf(\ n输入10位二进制Sting:);
获取(键); / *复制10位二进制字符串的表示形式


这是另一个你没有评论的问题。任何,任何,使用

函数gets()都是一个缺陷。它是

标准C库中唯一不能安全使用的功能,尤其是

不能用于交互式用户输入。


你认为会发生什么事情是用户在按Enter键之前键入40或50个字符

,并且gets()尝试将它们全部放入你的20

字符串?糟糕的事情会发生。

从键盘输入到key []。 不要用于二进制操作
只是表示* /

/ *下面我得到一个错误''char []''的间接等级与
''char''
和''=''左操作数必须是l值* /

key = P10(key); / *函数调用。将返回的数组复制到键[] * /


对,你不能分配给像''key''这样的数组,或者任何其他数组用于

这件事。


幸运的是,你不需要。当你将''key''传递给P10时,那个

函数实际上会收到一个指向key [0]的指针。任何更改

通过它接收的指针对键[0]和其他字符进行更改

更改原始数组的内容。所以你不需要

返回它,它只是随着函数的运行而改变。


所以这样做:

P10(关键);

printf(Key is now:\ n);
for(i = 0; i< 10; ++ i) {/ *循环打印返回的数组键[] * /
printf("%c\ n",key [i]);
}


....并且因为你要修复你的源来正确定义

main()作为返回int,在这里添加:


return 0; }

char P10(char Ctext []){/ *用于置换输入字符串的普通函数* /


更改为:


void P10(char Ctext [])


....或者更好地帮助避免将来出现混淆:


void P10(char * Ctext)

int Perm10 [10] = {3,5,2,7,4,10,1,9,8,6};
int i;
int index;

/ *跟踪语句检查键[]已通过* /
for(i = 0; i< 10; ++ i){<对于(i = 0; i< 10; i ++){
index = Perm10 [i];
printf(" index =%d \ n",index);
Ctext [i] = Perm10 [index-1 ];

}
/ *下面显示错误''return'':''char''的间接水平与''char *''* /


上面这个函数的主体实际上做了你想要它的b
,因为即使P10收到Ctext为指向char的指针,你可以在指针上使用下标。

返回Ctext; / *将Ctext [] / key []返回到调用函数* /


因为现在这是一个返回''void''的函数,所以只省略这一行。

}
Can someone please help, I''m trying to pass an array to a function, do some
operation on that array, then return it for further use. The errors I am
getting for the following code are, differences in levels of indirection, so
I feel it must have something to do with the way I am representing the array
in the call and the return.
That''s because you can''t pass or return bare arrays to or from
functions.
Below I have commented the problem parts.

Thanks in advance for any help offered.
Pete

#include <stdio.h>
#include <string.h>

char P10(char key[]); /* prototype */
This prototype is exactly equivalent to:

char P10(char *key);

....because the name of an array is always converted to a pointer to
its first element when used as an argument to a function. C allows
the notation that you used as a synonym to the one that I used. I
really wished that had never been allowed, because it only adds to the
array/pointer confusion that many newbies have.

And even if you could return an array from a function, which you
can''t, you have prototyped a function with a return type of a single
char only.

Actually, you want to change the return type of this function to void,
see my comments in the function itself.
void main()
This is a problem part that you neglected to comment. The only
standard, portable return type for main() in a hosted C environment is
''int''. No matter what the illiterate help files for some compilers
say.

int main()

....or even better:

int main(void)
{

char key[20];
int i;

printf("\nEnter a 10 bit Binary Sting: ");
gets(key); /* Copy representation of 10 bit binary string
Here''s another problem that you didn''t comment. Any, any, ANY use of
the function gets() is a defect. It is the only function in the
standard C library that can NEVER be used safely, and most especially
NOT TO BE used for interactive user input.

What do you think will happen is a user types 40 or 50 characters
before pressing enter, and gets() tries to put them all in your 20
character string? Bad things will happen.
entered from keyboard to key[]. "not to use for binary operations
just representation */

/* below I''m getting an error ''char[]'' differs in levels of indirection to
''char''
and ''='' left operand must be l-value */

key = P10(key); /* function call. copy returned array into key[] */
Right, you can''t assign to an array like ''key'', or any other array for
that matter.

Fortunately, you don''t need to. When you pass ''key'' to P10, that
function will actually receive a pointer to key[0]. Any change it
makes to key[0] and other characters via the pointer it receives
change the contents of the original array. So you don''t need to
return it, it just changes as the function runs.

So make this:

P10(key);
printf("Key is now : \n");
for(i=0; i<10; ++i) { /* loop to print returned array key[] */
printf("%c\n", key[i]);
}
....and since you are going to fix your source to properly define
main() as returning an int, add this here:

return 0; }
char P10(char Ctext[]) { /* Trivial function to permute input string */
Change to:

void P10(char Ctext[])

....or even better to help avoid confusion in the future:

void P10(char *Ctext)
int Perm10[10]={3,5,2,7,4,10,1,9,8,6};
int i;
int index;

/* Trace statement checking key[] has been passed */
for(i=0;i<10;++i) {
printf("Ctext = %c\n", Ctext[i]);
}
/* Trivial Permutation function */
for(i=0; i<10; i++) {
index = Perm10[i];
printf("index = %d\n", index);
Ctext[i] = Perm10[index-1];
}
/* Below shows the error ''return'' : ''char'' differs in
levels of indirection from ''char *'' */
The body of this function above here actually does what you want it to
do, because even though P10 receives Ctext as a pointer to char, you
can use subscripting on a pointer.
return Ctext; /* return Ctext[]/key[] to calling function */
Since this is now a function returning ''void'', just omit this line.
}




运行,不要走,到这个组的常见问题解答,链接我的签名,和

阅读全部关于指针和数组的整个部分。


然后得到一本好的C书,例如The C Programming Language,Second

Edition。作者:Kernighan& Ritchie。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C- faq / top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu .edu / ~a ... FAQ-acllc.html



Run, do not walk, to the FAQ for this group, link in my signature, and
read all of the entire section on pointers and arrays.

Then get a good C book, such as "The C Programming Language, Second
Edition" by Kernighan & Ritchie.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


你好,
但是让'' s说你不想修改传递的数组。那我们做什么呢?答案是

void P2(const char * Ctext,int N,char *输出)


size_t N不仅更好,甚至在最有意义时使用char

数组...... :-)

如果要允许Ctext和输出相同,则必须仔细编码。
However let''s say you do not want to modify the array passed. What do we do
then? The answer is

void P2(const char *Ctext, int N, char *output)
size_t N is not only nicer but even makes most sense when using char
arrays... :-)
If you are going to allow Ctext and output to be the same, you will have to
code carefully.




拼写出:实际的排列函数由OP提供的
是垃圾。我现在重新发布帖子并对其进行评论:


我输入Ctext [] =" 1111110000" ;;



Spell it out: The actual "permutation function" provided by the OP
is crap. I reordered the posting and comment on it now:

I enter with Ctext[]="1111110000";

char P10(char Ctext []){/ *用于置换输入字符串的普通函数* /
int Perm10 [10] = {3,5,2,7,4,10, 1,9,8,6};
int i;
int index;

/ *跟踪语句检查键[]已通过* /
for( i = 0; i< 10; ++ i){
printf(" Ctext =%c\ n",Ctext [i]);
}
/ * Trivial Permutation函数* /
for(i = 0; i< 10; i ++){
index = Perm10 [i];
i = 0:index = 3; printf(" index =%d \ n",index);
Ctext [i] = Perm10 [index-1];
Ctext [0] = Perm10 [3-1] = 2;

原始Ctext中从未有过两个。

}
/ *下面显示错误''return'':''char''的间接水平与''char *''* /

返回Ctext不同; / *将Ctext [] / key []返回到调用函数* /
}
char P10(char Ctext[]) { /* Trivial function to permute input string */

int Perm10[10]={3,5,2,7,4,10,1,9,8,6};
int i;
int index;

/* Trace statement checking key[] has been passed */
for(i=0;i<10;++i) {
printf("Ctext = %c\n", Ctext[i]);
}
/* Trivial Permutation function */
for(i=0; i<10; i++) {
index = Perm10[i]; i=0: index=3; printf("index = %d\n", index);
Ctext[i] = Perm10[index-1]; Ctext[0]=Perm10[3-1]=2;
There was never a two in the original Ctext.

}
/* Below shows the error ''return'' : ''char'' differs in
levels of indirection from ''char *'' */

return Ctext; /* return Ctext[]/key[] to calling function */
}




我不确定这是什么意思。我认为它应该是

的东西

Ctext [i] = Ctext [index-1];

但是,这是废话:想象一下非常简单的排列

{3,1,2}正在研究{''0'','0'',''1'}。这给了我们结果:

Ctext [0] = Ctext [3-1]; / * =''1''; * /

Ctext [1] = Ctext [1-1]; / * =''1''; * /

Ctext [2] = Ctext [2-1]; / * =''1''; * /

您可以通过一系列排列分解您的排列

的两个值并使用中间值进行交换/>
变量:

/ * Perm:{{3,2},{1,2}} * /

temp = Ctext [3-1 ]。 CTEXT [3-1] = CTEXT [2-1]; / * =''0''; * /

Ctext [2-1] = temp; / * =''1''; * /

temp = Ctext [1-1]; CTEXT [1-1] = CTEXT [2-1]; / * =''1''; * /

Ctext [2-1] = temp; / * =''0''; * /

或者你使用一个临时数组来编写你的排列的

结果。之后,您将

临时值复制到原始数组中:

tempArr [i] = Ctext [index-1];

in你的原始循环效果tempArr持有正确的排列

的Ctext。在返回之前,请执行

for(i = 0; i< 10; i ++)

Ctext [i] = tempArr [i];


尝试围绕从零开始的数组的想法和

基于零的排列来摆脱-1。

干杯

Michael



I am not sure what this is to mean. I think it should have been
something along the lines
Ctext[i]=Ctext[index-1];
However, this is crap, too: Imagine the very simple permutation
{3,1,2} working on {''0'',''0'',''1''}. This gives us the outcome:
Ctext[0]=Ctext[3-1]; /*=''1'';*/
Ctext[1]=Ctext[1-1]; /*=''1'';*/
Ctext[2]=Ctext[2-1]; /*=''1'';*/
Either you break up your permutation in a series of permutations
of two values and perform an exchange using an intermediate
variable:
/* Perm: {{3,2},{1,2}} */
temp=Ctext[3-1]; Ctext[3-1]=Ctext[2-1]; /*=''0'';*/
Ctext[2-1]=temp; /*=''1'';*/
temp=Ctext[1-1]; Ctext[1-1]=Ctext[2-1]; /*=''1'';*/
Ctext[2-1]=temp; /*=''0'';*/
or you use a temporary array to which you write the
outcome of your permutations. After that, you copy your
temporary values into the original array:
tempArr[i]=Ctext[index-1];
in your original loop effects tempArr holding the right permutation
of Ctext. Before returning, do
for (i=0; i<10; i++)
Ctext[i] = tempArr[i];

Try to wrap your mind arround the ideas of zero-based arrays and
zero-based permutations to get rid of the "-1".
Cheers
Michael


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

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