You are not logged in.

#1 2009-05-22 21:25:42

wankel
Member
From: Iowa, USA
Registered: 2008-05-30
Posts: 218
Website

c++ making an array of zeros?

Hi, Im trying to make an array that will be filled with data later, but I keep getting the following error when compiling:

error: incompatible types in assignment of of 'int*' to 'int[10]'

Im using the following line:

int counter[10]={0}

What needs to be changed? I read everywhere online and this is how they recommend doing it.

Offline

#2 2009-05-22 21:30:40

deej
Member
Registered: 2008-02-08
Posts: 395

Re: c++ making an array of zeros?

Try:

int counter[10];

This will initialize your array.

If you want it filled with zeroes:

int counter[] = { 0,0,0,0,0,0,0,0,0,0};

or:

int counter[10] = { 0,0,0,0,0,0,0,0,0,0};

Deej

Offline

#3 2009-05-22 22:32:29

Themaister
Member
From: Trondheim, Norway
Registered: 2008-07-21
Posts: 652
Website

Re: c++ making an array of zeros?

If there's, let's say an array of 1000, and you want it all initialized as zeros, is this the fastest way to do it?

int counter[1000];
for (int a = 0; a < 1000; a++) counter[a] = 0;

?

Offline

#4 2009-05-22 22:55:37

jsgt
Member
Registered: 2009-03-16
Posts: 29

Re: c++ making an array of zeros?

You could, but a better alternative is to use memset: memset( counter, 0, sizeof( counter ) )

Offline

#5 2009-05-23 01:45:48

kumyco
Member
From: somewhere
Registered: 2008-06-23
Posts: 153
Website

Re: c++ making an array of zeros?

you can do

int a[1000] = {0};

the rest will be init to 0

Offline

#6 2009-05-23 09:39:18

abesto
Member
From: Hungary
Registered: 2009-03-05
Posts: 49
Website

Re: c++ making an array of zeros?

This isn't strictly an answer to your question, but as we're talking about C++, why not use a vector? (or even a list if  it's one of those situations)

#include <vector>
...
vector<int> counter(10, 0);  // A vector of type int, size 10, each element is 0

Same thing for lists, just s/vector/list/g


Linux user #476135 || Dotfiles hosted by GitHub

Offline

#7 2009-05-23 13:40:49

kakTuZ
Member
From: Hannover, Germany
Registered: 2007-10-20
Posts: 86

Re: c++ making an array of zeros?

or use

int array[10];
std::fill( array, array+10, 0);

But using std::vector is a much better alternative (see previous post).

Last edited by kakTuZ (2009-05-23 13:42:02)

Offline

#8 2009-05-23 22:17:08

Lexion
Member
Registered: 2008-03-23
Posts: 510

Re: c++ making an array of zeros?

memset is better than fill, as it is more economic with data and CPU.


urxvtc / wmii / zsh / configs / onebluecat.net
Arch will not hold your hand

Offline

#9 2009-05-27 04:26:08

wankel
Member
From: Iowa, USA
Registered: 2008-05-30
Posts: 218
Website

Re: c++ making an array of zeros?

Hey guys, I tried all the suggestions provided and they all give me different kind of errors =/

Heres my code, "counter" is the array im trying to make that has a size of "height". can anyone see a reason why Id be getting errors trying to do this?


#include <stdio.h>
#include "CImg.h"
#include <vector>

using namespace cimg_library;


//main program
int main()
{
CImg<unsigned char> img("xray.jpg");

const unsigned int width = img.dimx();
const unsigned int height = img.dimy();

//converts the image to black and white
img.RGBtoYCbCr().channel(0).resize(-100,-100,1,3).RGBtoLUT(CImg<>(2,1,1,3).fill(0.0f,255.0f),false); 

//displays image
CImgDisplay display(img,"Black and White");
while (!display.is_closed)
display.wait();


printf("The height of the image is %d\n",height);
printf("The width of the image is %d\n",width);

//counter array
int counter[10];
std:fill( counter, counter+10, 0);

for (int i=0;i<width;i++)
    for (int j=0;j<height;j++)
        for (int k=0;k<3;k++)
            if (img(i,j,0,k)=0) {
            counter=counter+1;
            }    

printf("%d",counter);
return 0;
}

Offline

#10 2009-05-27 04:31:38

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,365
Website

Re: c++ making an array of zeros?

You start out treating counter as an array of length "height" - you probably want to use height not ten....:

//counter array
int counter[10];
std:fill( counter, counter+10, 0);

Then treat is like an int

counter=counter+1;

I guess you want:

coutner[j] += 1

An again:

printf("%d",counter);

You probably want to loop an print out each position.

Offline

#11 2009-05-27 05:49:30

the_isz
Member
Registered: 2009-04-14
Posts: 280

Re: c++ making an array of zeros?

Also, are you sure this condition is what you want?

if (img(i,j,0,k)=0)

This would assign the value of img at coordinate i,j,0,k the value 0 and return true every time that assignment
was successful (which should be always, except if you encounter some kind of index error or something).

You probably mean this:

if (img(i,j,0,k)==0)

Tip from my side: Get used to write constants first when doing comparisons. That way, the compiler will complain
if you forget a '=':

if (0=img(i,j,0,k))  // yields error
if (img(i,j,0,k)=0)  // compiles ok, but probably not what you expect
if (0==img(i,j,0,k)) // what you expect, more typing-error secure

I know, it takes time to get used to, but it helps avoiding such nasty mistakes wink

Offline

#12 2009-05-29 03:43:24

mfisher_ix
Member
Registered: 2009-05-08
Posts: 5

Re: c++ making an array of zeros?

wankel wrote:

int counter[10]={0}

Wankel, your original code should work (assuming the statement was correctly ended with a semicolon). A few questions:

- What compiler are you using, and what command are you using to compile?

- Are you sure the line number for the error refers to this statement?

Offline

#13 2009-05-31 22:34:12

wankel
Member
From: Iowa, USA
Registered: 2008-05-30
Posts: 218
Website

Re: c++ making an array of zeros?

mfisher_ix, this is the command im using to compile:

g++ -o beta.exe beta.cpp -O2 -L/usr/X11R6/lib -lm -lpthread -lX11

and im fairly certain thats the line where the error refers to

the_isz, thanks for the tips, ill definitely implement them smile

allan, thanks for the help! using 10 instead of height was just to test it out, in the end i want the counter to have "height" rows

Offline

Board footer

Powered by FluxBB