0

I've tried to create a 2D array where each row is going to hold a texture/image values (RGBA) (where each column of the texture will be placed after each other in a row). The base for my texture will be all white, therefore I set all values to 1. However, when I print out the value it is 0 and not 1. Why does data[layer][x*m_Size+y+a] = 1; not set the value of that position to 1?

data = new int*[m_Size];
for(unsigned i = 0; i<m_Size; ++i){
    data[i] = new int[m_Size*m_Size*4];
}
for(unsigned layer=0; layer<m_NumLayers; layer++){  
    for (unsigned x = 0; x < m_Size; x++){
        for (unsigned y = 0; y < m_Size*4; y+=4){
                data[layer][x*m_Size+y+r] = 1;  
                data[layer][x*m_Size+y+g] = 1;  
                data[layer][x*m_Size+y+b] = 1;  
                data[layer][x*m_Size+y+a] = 1;  
                printf("in data: %f \n,",data[layer][x*m_Size+y+a]);
        }

    }

}
3
  • If m_NumLayers is more than m_Size, then your loop will overflow data. Commented Jan 7, 2016 at 12:14
  • 4
    Your printf call is wrong. You want printf("in data: %d \n,",data[layer][x*m_Size+y+a]); instead of printf("in data: %f \n,",data[layer][x*m_Size+y+a]); Commented Jan 7, 2016 at 12:16
  • 1
    @ThorngardSO You should post that as an answer. Commented Jan 7, 2016 at 12:18

2 Answers 2

1

Your printf call is wrong. You want printf("in data: %d \n,",data[layer][x*m_Size+y+a]); instead of printf("in data: %f \n,",data[layer][x*m_Size+y+a]);, because you want to print an integer argument.

Sign up to request clarification or add additional context in comments.

Comments

0

You should use std::cout instead of printf. You see, you messed type modifier in printf - %f is for float, %d is for int.

Use cout instead:

#include <iostream>
...
std::cout << "in data: " << data[layer][x*m_Size+y+a] << std::endl;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.