在上面的代码段中,sum_hist是一个类型为long的双指针 int ...问题是当sum_hist的值打印时没有 是块...正在给出正确的结果..但是当外面的 循环......打印出来的值......错误的值被打印.....如何 更正代码...for(i=0;i<256;i++){hist[i]=0;}for(i=0;i<height;i++){for(j=0;j<width;j++){k=(long int)rgb1[i][j];hist[k]=hist[k]+1;}}for(i=0;i<256;i++){if(hist[i]!=0)tmp++;else continue;}sum_hist=(int**)malloc(tmp*sizeof(int*));for(i=0;i<tmp;i++)sum_hist[i]=(int*)malloc(2*sizeof(int));for(i=0;i<256;i++){if(hist[i]!=0){//printf("%d %d\n",i,hist[i]);sum_hist[i][0]=(int)i;sum_hist[i][1]=(int)hist[i];printf("%d %d\n",sum_hist[i][0],sum_hist[i][1]);}else continue;}for(i=0;i<tmp;i++)printf("%d\n",sum_hist[i][1]);In the above code segment sum_hist is a double pointer of type longint...the problem is that when the values of sum_hist is printed withnthe is block..it is giving correct result..but when outside the forloop..the value is printed......wrong values are printed.....how tocorrect the code...推荐答案 2008-09-13,biplab< ge ***** **@gmail.com写道:On 2008-09-13, biplab <ge*******@gmail.comwrote: for(i = 0; i< 256; i ++) { hist [i] = 0; }for(i=0;i<256;i++){hist[i]=0; } 您需要修改格式。建议在Usenet上发布2空格缩进 。在任何情况下,都要与保持一致,并在操作员周围使用空格。 此外,这可以替换为: memset (hist,0,256); 或者可能: memset(hist,0,sizeof hist);You need to fix your formatting. 2-space indents arerecommended for posting on Usenet. In any case, beconsistent, and use whitespace around operators.Also, this could be replaced with:memset(hist, 0, 256);Or potentially:memset(hist, 0, sizeof hist); for(i = 0; i< height; i ++) { for(j = 0; j< width; j ++) { k =(long int)rgb1 [i] [j];for(i=0;i<height;i++){for(j=0;j<width;j++){k=(long int)rgb1[i][j]; 为什么要演员?你在这里使用k的方式,它所有的逻辑 值都可以放入unsigned char中。当然那么它已经是什么类型的b $ b就足够了?Why this cast? The way you use k here, all its logicalvalues could fit into an unsigned char. Surely thenwhatever type it already was, is sufficient? hist [k] = hist [k] +1; } } for(i = 0; i< 256; i ++) { if(hist [i]!= 0) tmp ++; else continue;hist[k]=hist[k]+1;}}for(i=0;i<256;i++){if(hist[i]!=0)tmp++;else continue; else行是多余的。修复你的格式。The else line is superfluous. Fix your formatting. } sum_hist =(int **)malloc(tmp * sizeof(int *)) ;}sum_hist=(int**)malloc(tmp*sizeof(int*)); 不要强制转换malloc()。不要使用返回 malloc()而不检查它是否为NULL。不要将类型传递给 sizeof。Don''t cast the return of malloc(). Don''t use the return ofmalloc() without checking it for NULL. Don''t pass types tosizeof. for(i = 0; i< tmp; i ++) sum_hist [i] =(int *)malloc(2 * sizeof(int));for(i=0;i<tmp;i++)sum_hist[i]=(int*)malloc(2*sizeof(int)); 同上。Ditto. for(i = 0; i< 256; i ++) { if(hist [i]!= 0) { // printf("%d%d \ n",i,hist [i]);for(i=0;i<256;i++){if(hist[i]!=0){//printf("%d %d\n",i,hist[i]); 不要在usenet上使用单行注释,尤其是当你使用随机和过度的缩进方案时。 。 />Don''t use single-line comments on usenet, especially whenyou are using a random and excessive indentation scheme. sum_hist [i] [0] =(int)i; sum_hist [i] [1] =(int)hist [i] ;sum_hist[i][0]=(int)i;sum_hist[i][1]=(int)hist[i]; 这些演员阵容有什么用?What''s with these casts? printf("%d%d \ n",sum_hist [i] [0],sum_hist [i] [1]); } else继续;printf("%d %d\n",sum_hist[i][0],sum_hist[i][1]);}else continue; 此行是多余的。This line is superfluous. } for(i = 0; i< tmp; i ++) printf("%d \ n",sum_hist [i] [1]); 上面的代码段sum_hist是一个long类型的双指针 int ...问题是当sum_hist的值用$ 打印时,就是block..it是给出正确的结果..但是当外面的 循环...打印出来的值......错误的值被打印.....如何 更正代码...}for(i=0;i<tmp;i++)printf("%d\n",sum_hist[i][1]);In the above code segment sum_hist is a double pointer of type longint...the problem is that when the values of sum_hist is printed withnthe is block..it is giving correct result..but when outside the forloop..the value is printed......wrong values are printed.....how tocorrect the code... 修复上述问题并发布可编辑的细分部分代码那么 我们会看到问题所在。 - Andrew Poelstra ap ******* @ wpsoftware.com 要给我发电子邮件,请使用.com设置为的上述电子邮件地址.netFix the above issues and post a compilable "segment" of code. Thenwe will see what the problem is.--Andrew Poelstra ap*******@wpsoftware.comTo email me, use the above email addresss with .com set to .net 2008-09-13,biplab< ge ******* @ gmail.comwrote:On 2008-09-13, biplab <ge*******@gmail.comwrote: > < code snipped> 在上面的代码段中,sum_hist是long类型的双指针 int ...问题是,当sum_hist的值打印出来时, 是块...它给出了正确的结果..但是当在 $之外时b $ b loop ..打印的值......打印错误的值.....如何 更正代码...><code snipped>In the above code segment sum_hist is a double pointer of type longint...the problem is that when the values of sum_hist is printed withnthe is block..it is giving correct result..but when outside the forloop..the value is printed......wrong values are printed.....how tocorrect the code... 这是你的代码,针对(主要是文体)问题进行了更正我提到了b $ b提到的其他问题。现在我非常清楚 问题是什么。我希望你也清楚: / *开始无法编译的段* / memset(hist,0,256); for(i = 0; i< height; i ++) for(j = 0; j< width; j ++) { k = rgb1 [i] [j]; if(hist [k] == 0) ++ count ; ++ hist [k]; } sum_hist = malloc(count * sizeof * sum_hist); i $ 0; i< count; i ++) sum_hist [i] = malloc(2 * sizeof * sum_hist [i]); for(i = 0; i< 256; i ++) if(hist [i]!= 0) { / * printf("%d%d \ n",i,hist [i]); * / sum_hist [i] [0] = i; sum_hist [i] [1] = hist [i]; printf ("%d%d \ n",sum_hist [i] [0],sum_hist [i] [1]); } for(i = 0; i< count; i ++) printf("%d \ nn",sum_hist [i] [1]); / *结束段* / - Andrew Poelstra ap ******* @ wpsoftware.com 要给我发电子邮件,请使用.com设置为.net 的上述电子邮件地址Here is your code, corrected for the (mainly stylistic) issues Imentioned else thread. It is abundantly clear to me now what theproblem is. I hope it is also clear to you:/* Begin uncompilable segment */memset(hist, 0, 256);for(i = 0; i < height; i++)for(j = 0; j < width; j++){k = rgb1[i][j];if(hist[k] == 0)++count;++hist[k];}sum_hist = malloc(count * sizeof *sum_hist);for(i = 0; i < count; i++)sum_hist[i] = malloc(2 * sizeof *sum_hist[i]);for(i = 0; i < 256; i++)if(hist[i] != 0){/* printf("%d %d\n", i, hist[i]); */sum_hist[i][0] = i;sum_hist[i][1] = hist[i];printf("%d %d\n", sum_hist[i][0], sum_hist[i][1]);}for(i = 0; i < count; i++)printf("%d\n", sum_hist[i][1]);/* End segment */--Andrew Poelstra ap*******@wpsoftware.comTo email me, use the above email addresss with .com set to .net 2008-09-13,Andrew Poelstra< ap ******* @ supernova.homewrote:On 2008-09-13, Andrew Poelstra <ap*******@supernova.homewrote: On 2008-09-13,biplab< ge ******* @ gmail.comwrote:On 2008-09-13, biplab <ge*******@gmail.comwrote: >> < code snipped> ; 在上面的代码段中,sum_hist是long int类型的双指针...问题是当sum_hist的值打印时,是块。 .it给出了正确的答案结果..但是当外面的循环......打印出来的值......打印错误值.....如何更正代码...>><code snipped>In the above code segment sum_hist is a double pointer of type longint...the problem is that when the values of sum_hist is printed withnthe is block..it is giving correct result..but when outside the forloop..the value is printed......wrong values are printed.....how tocorrect the code... 这是你的代码,针对(主要是文体)问题进行了更正我提到了b $ b提到的其他线程。现在我非常清楚 问题是什么。我希望你也清楚: / *开始无法编译的段* / memset(hist,0,256); for(i = 0; i< height; i ++) for(j = 0; j< width; j ++) { k = rgb1 [i] [j]; if(hist [k] == 0) ++ count ; ++ hist [k]; } sum_hist = malloc(count * sizeof * sum_hist); i $ 0; i< count; i ++) sum_hist [i] = malloc(2 * sizeof * sum_hist [i]);Here is your code, corrected for the (mainly stylistic) issues Imentioned else thread. It is abundantly clear to me now what theproblem is. I hope it is also clear to you:/* Begin uncompilable segment */memset(hist, 0, 256);for(i = 0; i < height; i++) for(j = 0; j < width; j++) { k = rgb1[i][j]; if(hist[k] == 0) ++count; ++hist[k]; }sum_hist = malloc(count * sizeof *sum_hist);for(i = 0; i < count; i++) sum_hist[i] = malloc(2 * sizeof *sum_hist[i]); 我忘了添加malloc()不返回NULL的检查。I forgot to add checks that malloc() does not return NULL. for(i = 0; i< 256 ; i ++) if(hist [i]!= 0) { / * printf("%d%d \ n,i,hist [i]); * / sum_hist [i] [0] = i;for(i = 0; i < 256; i++) if(hist[i] != 0) { /* printf("%d %d\n", i, hist[i]); */ sum_hist[i][0] = i; 对于OP,当我超过计数时会发生什么?To the OP, what happens when i exceeds count? sum_hist [i] [1] = hist [i]; printf("%d%d \ n",sum_hist [i] [0],sum_hist [i] [1]); } for(i = 0; i< count; i ++) printf("%d \ n",sum_hist [i ] [1]); / *结束段* / sum_hist[i][1] = hist[i]; printf("%d %d\n", sum_hist[i][0], sum_hist[i][1]); }for(i = 0; i < count; i++) printf("%d\n", sum_hist[i][1]);/* End segment */ - Andrew Poelstra ap*******@wpsoftware.com 给我发电子邮件,使用.com设置为.net--Andrew Poelstra ap*******@wpsoftware.comTo email me, use the above email addresss with .com set to .net 这篇关于如何纠正代码段?????????????的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-03 06:01