You are not logged in.
Pages: 1
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
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
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
You could, but a better alternative is to use memset: memset( counter, 0, sizeof( counter ) )
Offline
you can do
int a[1000] = {0};
the rest will be init to 0
Offline
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
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
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
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
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
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
Offline
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
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
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
Pages: 1