【问题标题】:Passing a pointer to an array in a different function in C在C中的不同函数中传递指向数组的指针
【发布时间】:2009-06-04 18:09:03
【问题描述】:

我正在尝试传递一个用 memset 初始化为 0 的指针 rgb,然后循环将一个 32 位整数仅放置在我使用高度和宽度输入(h 和 w)以及从二维数组(x 和 y)的左上角。编译后,我似乎在指针的 printf 值在它生成之后给出了正确的值(在我的情况下,0xFFFFFF 和 r g b 的输入为 255 255 255)但是在它传递给 rgb2yuv 函数之后,它当我在那里 printf 时设置为 0。

任何建议都会很棒!

#include <stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<string.h>

/*global definitions*/
#define WIDTH 480
#define HEIGHT 240

/*global declarations*/
int w,h,scrn, bytewrite;
unsigned char  red, green, blue;
static unsigned long rgb[WIDTH][HEIGHT];


/*function declarations*/
void colorq();
void rgb_rectdraw(int x, int y, int w, int h, unsigned char red , 
    unsigned char green, unsigned char blue, unsigned long *rgb);
void rgb2yuv(unsigned long *rgb);


/*
   Function Name: main
   Purpose: main function
*/

int main(int argc, char** argv){
printf("\n");
int x, y;    

/*call colorq to create a 32bit number of RGB*/
colorq();    

/ *call rgb_rectdraw to draw a rectangle RGB array*/
rgb_rectdraw(x, y, w, h, red, green, blue, rgb);

/*call rgb2yuv to take the RGB array and covert it to a YUV array*/
rgb2yuv(rgb);    
return 0;
}

/*
  Function name: color q
  Purpose: asks user to input colors from 0 to 255 in RGB format
*/

void colorq(){    
printf("Please enter a color for Red Green and Blue from 0 to 255:\n");
scanf("%hu", &red);
scanf("%hu", &green);
scanf("%hu", &blue);
printf("\n");    
return;
}        

/*
   Function name: rectdraw
   Purpose: Draws a rectangle array
*/

void rgb_rectdraw(int x, int y, int w, int h,unsigned char red, 
    unsigned char green,  unsigned char blue,unsigned long  *rgb){
unsigned long rgbpixel;

/* testing only take out when 
   finished debugging why red is always 0 after scanf */
red = 255;
printf("red set to 255 for debugging\n");

/*creates a 32-bit number of RGB*/
rgbpixel = (red<<16)|(green<<8)|blue;
printf("%#x\n",rgbpixel);

/*create array of width w height h*/ 
/*initialize array*/

memset (rgb, 0,sizeof(HEIGHT*WIDTH));
int i, j, startx, stopx, starty, stopy;


printf("enter width and height of rectangle in pixels\n");
scanf("%d %d", &w, &h);

printf("enter offset x pixels and y pixels of rectangle:\n");
scanf("%d %d", &x, &y);

startx=x;
starty=y;
stopx=x+w;
stopy=y+w;

/* creates array of w and h of int rgh */ 
for(i=startx; i <= stopx; i++){
   for(j=starty; j <= stopy; j++){
      rgb = rgbpixel;
      }
   j = 0;
   }
printf("original rgb %#x\n",rgb);
return ;
}

/*
 *Function Name: rgb2yuv
 *Purpose: convert the RGB array to a YUV array
 */

void rgb2yuv(unsigned long  *rgb){    
int i,j;
printf("ptrpassed = %#x\n",*rgb);
for(i=0; i<=WIDTH;i++){
   for(j=0; j<=HEIGHT; j++){
   }
}
printf("\n");
return;
}

【问题讨论】:

  • 有人可以重新格式化问题吗?
  • 看这段代码好痛苦!您能否编辑它并将代码粘贴为代码示例。干杯。
  • @bryce:停止编辑。我已经修复了 5 次。
  • 很抱歉,刚接触该网站,不习惯在此处发布代码。谢谢!
  • @bryce:不用担心:代码块在一行之前有 4 个空格。

标签: c arrays pointers function


【解决方案1】:

我检查并基本上整理了所有的疣,并解释了原因。其中很大一部分是这样的事实:如果你的编译器发出警告,你必须听它们

/* Changed: Code formatted for my sanity */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

/*global definitions*/
#define WIDTH 480
#define HEIGHT 240

/*global declarations*/
int w,h,scrn, bytewrite;
/* Changed: to a short because I don't like the fact that I might be overwriting
   memory on accident (in colorq). */
unsigned short red, green, blue;
static unsigned long rgb[WIDTH][HEIGHT];

/* Changed: Some format strings have been changed to get rid of compiler
   warnings. */

/*function declarations*/
void colorq();
/* Changed: Specify the second dimension when you're passing an array. */
void rgb_rectdraw(int x, int y, int w, int h, unsigned char red,
                  unsigned char green, unsigned char blue,
                  unsigned long rgb[][HEIGHT]);
/* Changed: always pass an array of arrays. */
void rgb2yuv(unsigned long rgb[][HEIGHT]);


/*
   Function Name: main
   Purpose: main function
 */

int main(int argc, char** argv)
{
    printf("\n");
    int x, y;


    /*call colorq to create a 32bit number of RGB*/
    colorq();


    /* call rgb_rectdraw to draw a rectangle RGB array */
    rgb_rectdraw(x, y, w, h, red, green, blue, rgb);

    /* call rgb2yuv to take the RGB array and covert it to a YUV array */
    rgb2yuv(rgb);

    return 0;
}

/*
   Function name: color q
   Purpose: asks user to input colors from 0 to 255 in RGB format
 */

void colorq(){
    /* Suggestion: restructure this method to just take in all its input
       locally, then return a rgbpixel. */

    printf("Please enter a color for Red Green and Blue from 0 to 255:\n");
    scanf("%hu", &red);
    scanf("%hu", &green);
    scanf("%hu", &blue);
    printf("\n");

    return;
}



/*
   Function name: rectdraw
   Purpose: Draws a rectangle array
 */

void rgb_rectdraw(int x, int y, int w, int h,unsigned char red,
                  unsigned char green, unsigned char blue,
                  unsigned long rgb[][HEIGHT])
{
    unsigned long rgbpixel;

    /* testing only take out when
       finished debugging why red is always 0 after scanf */
    red = 255;
    printf("red set to 255 for debugging\n");

    /*creates a 32-bit number of RGB*/
    /* Changed: Added the extra 0xFF masking because of shortness rather than
       charness. */
    rgbpixel = ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | (blue & 0xFF);
    printf("%#lx\n",rgbpixel);

    /*create array of width w height h*/
    /*initialize array*/

    /* Changed: fill the size of one element times the number of elements */
    memset(rgb, 0, sizeof(unsigned long) * HEIGHT * WIDTH);
    int i, j, startx, stopx, starty, stopy;


    printf("enter width and height of rectangle in pixels\n");
    scanf("%d %d", &w, &h);

    printf("enter offset x pixels and y pixels of rectangle:\n");
    scanf("%d %d", &x, &y);

    startx=x;
    starty=y;
    stopx=x+w;
    stopy=y+w;

    /* creates array of w and h of int rgh */
    for(i=startx; i <= stopx; i++){
        for(j=starty; j <= stopy; j++){
            rgb[i][j] = rgbpixel;
        }
        j = 0;
    }
    printf("original rgb %#lx\n", (long unsigned int) rgb);
    return ;
}

/*
 *Function Name: rgb2yuv
 *Purpose: convert the RGB array to a YUV array
 */

void rgb2yuv(unsigned long  rgb[][HEIGHT]){

    int i,j;
    /* Changed: You can't just dereference rgb twice -- you have to use array
       notation here. */
    printf("ptrpassed = %#lx\n", rgb[0][0]);
    for(i=0; i<=WIDTH; i++){
        for(j=0; j<=HEIGHT; j++){
        }
    }
    printf("\n");
    return;
}

【讨论】:

  • 感谢 Paul 和所有提供帮助的人!
  • @Paul:哇,你手头有很多时间。如果您正在寻找一些与代码无关的工作,我需要为我的车库涂漆......
【解决方案2】:

这实际上有许多错误,但您的第一个问题是将像素值分配给数组:

   for(i=startx; i <= stopx; i++){
   for(j=starty; j <= stopy; j++){
      rgb = rgbpixel;
      }
   j = 0;
   }

你的意思可能是这样的:

rgb[i][j] = rgbpixel;

您不需要将 j 重置为 0——无论如何,内部的 for 循环都会立即将 j 重置为 starty。

另外,您误用了 sizeof()。您可能想要 sizeof(rgb) 代替。

【讨论】:

    【解决方案3】:

    为什么不使用结构?如果代码没有可移植的意图,您可以轻松逃脱:

    struct rgb_bits {
        int red   : 8;
        int green : 8;
        int blue  : 8;
    };
    union rgb {
        rgb_bits colour;
        long     array;
    }
    

    消隐像素然后就变成了:

    union rgb pixel;
    pixel.array = 0;
    

    设置单个颜色变为:

    union rgb pixel;
    pixel.colour.red   = ...;
    pixel.colour.green = ...;
    pixel.colour.blue  = ...;
    

    【讨论】:

      【解决方案4】:

      看起来您将单个像素值存储在指向您的数据的指针中:

      rgb = rgbpixel;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-09
        • 2012-03-22
        • 2016-06-16
        • 1970-01-01
        • 1970-01-01
        • 2015-08-05
        • 1970-01-01
        相关资源
        最近更新 更多