C ++使用指针,什么也没有传递 [英] C++ using pointers, nothing is passed

查看:77
本文介绍了C ++使用指针,什么也没有传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码已执行,但是image_data上未传递任何内容(write_function第44行)

Code executes, but nothing gets passed on image_data (line 44 the write_function)

所以我没有该功能的工作数组(第11行),为什么会这样呢?我认为传递image_data的引用应该足够了吗? (我已经习惯了,主要是开发JAVA)

So I have no working array for the function (line 11), why is that so? I thought passing on the reference for image_data should be enough? (I'm used to it, mostly developing JAVA)

#include <iostream>
#include <string.h>
#include <fstream>

using namespace std;

//Expects bgra image data
//Only makes bgr and bgra with 8 bits per pixel pictures
//Expects big endian ushorts
//makes no checks on the data supplied!
void write_tga(ofstream& output,char image_data[],unsigned short xWidth,unsigned short yWidth,bool transparency) //Line 11 - Line 11 - Line 11 - Line 11
{
    char zero = static_cast<char>(0x00);
    char two = static_cast<char>(0x02);

    //Convert uint to char Array
    unsigned char xWidth_arr[2];
    unsigned char yWidth_arr[2];
    memcpy(xWidth_arr, &xWidth, 2);
    memcpy(yWidth_arr, &yWidth, 2);

    char header[18] = {zero,zero,two,zero,zero,zero,zero,zero, zero,zero,zero,zero, xWidth_arr[0],xWidth_arr[1],yWidth_arr[0],yWidth_arr[1],static_cast<char>(0x18),zero};
    //enabling transparency
    if(transparency){
        header[16]= static_cast<char>(0x20);
        header[17]= static_cast<char>(0x08);
    }
    char footer[26] = {zero,zero,zero,zero,zero,zero,zero,zero, 'T','R','U','E','V','I','S','I','O','N','-','X','F','I','L','E','.',zero};

    output.write((char*)&header,sizeof(header));
    output.write((char*)&image_data,sizeof(image_data));
    output.write((char*)&footer,sizeof(footer));
    output.close();
    cout << image_data[0] << endl;
}

int main()
{
    ofstream output("data.tga",ios::binary);
    output.seekp(0);

    char zero = static_cast<char>(0x00);
    char image_data[12] = {static_cast<char>(0xff),static_cast<char>(0xff),static_cast<char>(0xff), zero,zero,zero, zero,zero,zero, zero,zero,zero};

    write_tga(output,image_data,2,2,false); //Line 44 - Line 44 - Line 44 - Line 44
    return 0;
}

推荐答案

您到底要在这段代码中尝试做什么:

What exactly are you trying to do in this code:

output.write((char*)&image_data,sizeof(image_data));

数组名称是一个指针.您正在将其内容(内存地址)写入output,而不是其指向的实际数据.删除&运算符以写入它指向的内容,我想这就是您想要的.或在第一个成员上使用&(例如:(char*)&image_data[0]).

Array name is a pointer. You're writing to output its content (the memory address), not the actual data it points to. Remove the & operator to write the content it points to, which is what you wanted, I think. Or use & on the first member (e.g.: (char*)&image_data[0]).

sizeof返回image_data的指针大小.使用尺寸"参数来传达尺寸.

sizeof returns a size of a pointer for image_data. Use a "size" parameter to convey the size.

并且您正在滥用static_cast,没有明显的原因.

And you're abusing static_cast for no apparent reason.

这篇关于C ++使用指针,什么也没有传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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