将指针传递给C中的另一个函数中的数组。

时间:2021-12-28 21:42:25

I am trying to pass a pointer rgb that is initialized with memset to 0 and then looped through to place a 32 bit integer only in the bounds that I create with height and width input (h and w) as well as offset from the top left corner of the 2d array (x and y). after compiling, I seem to have the value with printf of the pointer just after it was made which gives the correct value (in my case 0xFFFFFF with and input of 255 255 255 for r g b) but after it is passed through to rgb2yuv function, it is set to 0 when I printf there.

我想传递一个指针初始化的rgb memset为0,然后循环通过将一个32位整数只有在我创建的界限高度和宽度输入(h和w)以及抵消从左上角的2 d数组(x,y)。在编译之后,我似乎有价值printf的指针后它是给正确的值(在我的情况下0 xffffff和输入的255 255 255 r g b)但是rgb2yuv函数是通过之后,我printf的时候设置为0。

Any suggestions would be awesome!

任何建议都是非常棒的!

#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;
}

4 个解决方案

#1


2  

I went through and basically sorted out all the warts, and explained why. A lot of it amounts to the fact that if your compiler spits out warnings, you have to listen to them.

我检查了所有的疣,并解释了原因。很多情况下,如果编译器发出警告,就必须监听它们。

/* 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;
}

#2


2  

This actually has a number of bugs, but your first issue is assigning the pixel value to the array:

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

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

You probably meant something like this:

你的意思大概是这样的:

rgb[i][j] = rgbpixel;

You don't need to reset j to 0 -- the inner for-loop will immediately reset j to starty anyhow.

你不需要将j重置为0——无论如何,内循环将立即重置j到starty。

Also, you're misusing sizeof(). You probably want sizeof(rgb) instead.

同样,你滥用sizeof()。您可能需要sizeof(rgb)。

#3


1  

Why not use a structure? If the code has no intention of being portable you could easily get away with:

为什么不使用结构呢?如果代码没有任何可移植的意图,您可以轻松地离开:

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

Blanking out the pixel then simply becomes:

去掉像素就变成:

union rgb pixel;
pixel.array = 0;

and setting individual colours becomes:

设置个人色彩成为:

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

#4


0  

Looks like you're storing your single pixel value in the pointer to your data to me:

看起来你将你的单个像素值存储到你的数据的指针中:

rgb = rgbpixel;

#1


2  

I went through and basically sorted out all the warts, and explained why. A lot of it amounts to the fact that if your compiler spits out warnings, you have to listen to them.

我检查了所有的疣,并解释了原因。很多情况下,如果编译器发出警告,就必须监听它们。

/* 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;
}

#2


2  

This actually has a number of bugs, but your first issue is assigning the pixel value to the array:

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

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

You probably meant something like this:

你的意思大概是这样的:

rgb[i][j] = rgbpixel;

You don't need to reset j to 0 -- the inner for-loop will immediately reset j to starty anyhow.

你不需要将j重置为0——无论如何,内循环将立即重置j到starty。

Also, you're misusing sizeof(). You probably want sizeof(rgb) instead.

同样,你滥用sizeof()。您可能需要sizeof(rgb)。

#3


1  

Why not use a structure? If the code has no intention of being portable you could easily get away with:

为什么不使用结构呢?如果代码没有任何可移植的意图,您可以轻松地离开:

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

Blanking out the pixel then simply becomes:

去掉像素就变成:

union rgb pixel;
pixel.array = 0;

and setting individual colours becomes:

设置个人色彩成为:

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

#4


0  

Looks like you're storing your single pixel value in the pointer to your data to me:

看起来你将你的单个像素值存储到你的数据的指针中:

rgb = rgbpixel;