本文介绍了传递和返回函数的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 有人可以请求帮助,我正在尝试将数组传递给函数,对该数组执行一些 操作,然后将其返回以供进一步使用。对于以下代码,我得到的错误是: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 someoperation on that array, then return it for further use. The errors I amgetting for the following code are, differences in levels of indirection, soI feel it must have something to do with the way I am representing the arrayin 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 stringentered from keyboard to key[]. "not to use for binary operationsjust 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 inlevels 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 atabout the same time as one-dimensional arrays. This is IMO a mistake. In C,one-dimensional arrays are basic, but multi-dimensional arrays are advancedfeatures.What does this have to do with you, since you only use a 1 dimensionalarray?The answer is that the primer obscures the truth that C does not pass orreturn arrays to functions. It passes the address of the first element ofthe array.Your function should therefore bechar P10(char *Ctext)However virtually any programmer would want some security about the lengthof the array (and not just rely on the caller reading the comments andguessing from the name that P10 takes 10 characters)So the prototype becomes/*P2 - permute an arrayCtext - text to permuteN - 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 permutedin place. There is thus no need to return anything. So the function becomesvoid P2(char *Ctext, int N)However let''s say you do not want to modify the array passed. What do we dothen? The answer isvoid P2(const char *Ctext, int N, char *output)If you are going to allow Ctext and output to be the same, you will have tocode carefully. 运行,不要走,到这个组的常见问题解答,链接我的签名,和 阅读全部关于指针和数组的整个部分。 然后得到一本好的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.htmlRun, do not walk, to the FAQ for this group, link in my signature, andread all of the entire section on pointers and arrays.Then get a good C book, such as "The C Programming Language, SecondEdition" by Kernighan & Ritchie.--Jack KleinHome: http://JK-Technology.ComFAQs forcomp.lang.c http://www.eskimo.com/~scs/C-faq/top.htmlcomp.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 拼写出:实际的排列函数由OP提供的是垃圾。我现在重新发布帖子并对其进行评论: 我输入Ctext [] =" 1111110000" ;;Spell it out: The actual "permutation function" provided by the OPis crap. I reordered the posting and comment on it now:I enter with Ctext[]="1111110000"; 我不确定这是什么意思。我认为它应该是 的东西 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。 干杯 MichaelI am not sure what this is to mean. I think it should have beensomething along the linesCtext[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 permutationsof two values and perform an exchange using an intermediatevariable:/* 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 theoutcome of your permutations. After that, you copy yourtemporary values into the original array:tempArr[i]=Ctext[index-1];in your original loop effects tempArr holding the right permutationof Ctext. Before returning, dofor (i=0; i<10; i++)Ctext[i] = tempArr[i];Try to wrap your mind arround the ideas of zero-based arrays andzero-based permutations to get rid of the "-1".CheersMichael 这篇关于传递和返回函数的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!