Pointers and arrays

09 May 2015

Hi. I Posted this under questions, but the questions section seems to be being bombed by "Abam26.com" at the moment, so I figured I'd best re-post here. I suspect this sort of thing gets asked a lot, but I've read through everything I could find about it, and still couldn't work it out, so I figured my best bet was to ask. I'm trying to create a menu system to select which of a selection of boolean arrays gets plotted to an LCD screen. I've got a working general menu function, which accepts menus in the form of arrays (EG; const char* mainMenu[]= {"thing", "other thing", "doofer", "thingamijig",0}; (the 0 at the end of the array's a bodge to allow the size of the array to be calculated when the array is passed through to the function, and decays to a pointer)), and outputs an integer value corresponding to the selected term (EG, if you selected doofer, it would return a value of 2). This works fine, with no problems. I figured the best way to select the arrays to be plotted would be to create an integer array of pointers to said arrays, which I attempted, and which failed. Since I've not tried using pointers much before, I decided to go back to basics, and check that I could make them work in any way at all. I tagged the following lines of code to the start of my program; int foo; int bar = &foo; So far as I can understand, this should generate the integer foo, and then set the following integer bar to contain the memory adress of foo. however, when I try to do this, I get the following error: Error: A value of type "int *" cannot be used to initialize an entity of type "int" in "main.cpp"

The only way I've found to avoid this error coming up is to set bar as a boolean value, which surely makes no sense; the adress of "foo" can't be "true" or "false". Anyone any ideas of what is going on?

09 May 2015

&foo is a pointer to an int, not an int. Therefore you must declare bar as a pointer to an int, not an int.

i.e. int* bar = &foo;

If you haven't done so already, read Chapter 5 Pointers and Arrays in "The C Programing Language" by Brian W. Kernighan and Dennis M. Ritchie.