4

[SOLVED]

I'm using CodeBlocks (C++) on Win10. I tried to create a simple graphic program - I downloaded a certain graphics package (graphics.h and other two files related to BGI - I added the lib in settings etc.). I tried this program but there seems to be a problem at the line marked. I ported this from Pascal (as my teacher -yes, it's about college-, only shows us Pascal programs) in a correct manner I suppose, or at least partially. The problem is certainly with the pointers (EDIT AFTER SOLVING: it wasn't!, check my answer). I'll give more details if needed.

The Question: Where I did mistakes and how to correct them (what to write, what to add, what to delete) so that it wil work fine? If there is no mistake in the code, but I NEED some files for graphics to work on my compiler, which are the files, where to get them and where to put them?

ERROR (not quite, now's a "warning"):

deprecated conversion from string constant to 'char*' [-Wwrite-strings]

(see code a bit below)

Please read: I would BE VERY GRATEFUL for a fixed version of my program. Refferences are USELESS, so unless you REALLY want to help me (thank you!), leave this page. My only related capacity is to compare two versions of a program that are intended to do the same thing but one has a mistake (or more).

CODE (updated!):

#include<iostream>
#include<graphics.h>
#include<conio.h>
using namespace std;
int main(){
int gr,xmax,ymax,r;
int gm,gd;
gd=DETECT;
gm=0;
initgraph(&gd, gm, "C:\\TC\\BGI"); /*edit(solved): followed the tutorial linked in my answer; not a directory in my PC.*/
gr=graphresult();
if(gr!=grOk) cout<<"Error!";
    else    {xmax=getmaxx();
            ymax=getmaxy();
            cout<<"Resol.: "<<xmax+1<<"x"<<ymax+1;}
setcolor(7);rectangle(0,0,xmax,ymax);setcolor(5);line(0,0,xmax,ymax);line(0,ymax,xmax,0);setcolor(3);
for(r=(ymax+1)/2;r>=0;r--) circle((xmax+1)/2,(ymax+1)/2,r);
getch();
closegraph();
return 0;
}

graphics.h source and guide: http://www.codewithc.com/how-to-include-graphics-h-in-codeblocks/

genpfault
  • 51,148
  • 11
  • 85
  • 139
Mihai Florea
  • 63
  • 1
  • 2
  • 8
  • I heard I need some .bgi files - drivers (the third "argument" in init is related to them) . I don't have any. I could try putting them in the directory of the program (as " " from what I know will search for .bgi-s in the directory the program (mostly the .cpp) is in. ) – Mihai Florea Mar 15 '17 at 07:10
  • -got egavga.bgi - – Mihai Florea Mar 15 '17 at 11:14
  • Please, remove the `*` in the declaration of `gm`. It's wrong. If `DETECT` does not work you could try `gd = VGA; gm = VGAHI;` (or the resp. integer constants). And insert the `&` before `gm` in the call of `initgraph()`. – Scheff's Cat Mar 15 '17 at 11:45
  • onto it , result in 2 mins – Mihai Florea Mar 15 '17 at 11:47
  • Bigger error than before http://i66.tinypic.com/1zx0bjd.jpg – Mihai Florea Mar 15 '17 at 11:54
  • What error? Did you see my comment? (I added something after I sent it first.) – Scheff's Cat Mar 15 '17 at 11:57
  • I renamed some personal data, that's why the program appears to be unsaved, btw. -Yes, I saw it. – Mihai Florea Mar 15 '17 at 12:00
  • Sorry, our company proxy prohibits access to tinypic.com. The text would be welcome. – Scheff's Cat Mar 15 '17 at 12:00
  • so, I'll throw the code and error from your version of my program (with those corrections made) on pastebin, since it didn't improve anything. – Mihai Florea Mar 15 '17 at 12:04
  • http://pastebin.com/D4VcX3nV – Mihai Florea Mar 15 '17 at 12:08
  • Saw you code on paste bin. Now, the declaration of `gd` is wrong. Please, remove the `*`. – Scheff's Cat Mar 15 '17 at 12:16
  • did that @Scheff . --> http://pastebin.com/szazKDM6 I'll be back on the thread tomorrow. Have a nice day. – Mihai Florea Mar 15 '17 at 12:20
  • This is because a string constant is passed to a `char*` parameter. You may search the compiler option to switch this warning off. (As I already told you: BGI is _really_ old and may not fully fit into the newest C standards. But It's actually only a warning.) Btw. I edited my 1st answer to show you the correctly fixed code. – Scheff's Cat Mar 15 '17 at 12:24
  • just a silly point but looks like you are dealing with Borland BGI which is only for MS-DOS compliant OS and running in Win10 which has no MS-DOS so unless you are running in emulator (like DosBox) or using different BGI lib (like WinGDI instead of ancient Borland graphics.h) it will not work no matter what you try. Also you should have the correct BGI driver for your gfx card usually EGA/VGA/VESA works on majority of gfx cards (even if emulated these days) but I have seen cards that do not support VESA without proper OS driver installed (even on MS-DOS) before. – Spektre Mar 16 '17 at 06:14
  • I will check my PC on sunday (I'm not currently at home) and try to get WinGDI, and if I can't, I'll run CodeBlocks in DosBox (which I already had installed so I could play my beloved Dune 2). I'll announce if anything changes in the current situation. – Mihai Florea Mar 18 '17 at 05:49
  • [And I need to mention: I don't ask for corrections so I could go to college and show how I finally fixed the problem, and be applauded. I need corrections so I can learn what went wrong and how to fix it in the future.] – Mihai Florea Mar 18 '17 at 05:55
  • @Spektre Well... I can't seem to find a download page for WinGDI, aand, I tried running my program (exe) in DosBox but "It can't be run in dos-mode". EDIT: found this: https://github.com/ufoai/ufoai.codeblocks/blob/master/MinGW/include/wingdi.h . Wait! It was already in include =] – Mihai Florea Mar 19 '17 at 16:05
  • WinGDI is a total error waterfall. I don't think it's made for C(++) - errors in WinGDI.h appear regarding some declarations like UINT which are not recognised :/ – Mihai Florea Mar 19 '17 at 16:16
  • @MihaiFlorea you need to set your compiler/linker to target 16 bit MS-DOS platform otherwise you can not run your exe in MS-DOS at all. may be it will be easier to code directly in MS-DOS (like in old Borland Turbo C++) otherwise you can hit a lot of problems like unable to link old 16 bit PE or OBJ files together in your new environment ... Your best choice however is either abandon BGI or use older OS and tools... unless you are not bound to any of them by assignment. – Spektre Mar 19 '17 at 19:38

5 Answers5

0

You get an error because you try to take the address of something that isn't stored in memory.

Character literals like ' ' are basically translated into integers by the compiler, and you would not expect e.g. &32 (the ASCII code for space) to work would you?

If you're supposed to pass a string, use double-quotes and no address-of operator, as in " ".

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • " " results in " Invalid conversion from int to int* " and the log says " initialising argument 1 of .." poiting to this line of graphics.h: void initgraph( int * graphdriver, int * graphmode, char * pathtodriver ); . Actually, it was pretty valid info - literals don't have adresses (or have they? anyway, this isn't the focus of the question) So the .h is faulty... (I moved my comms) – Mihai Florea Mar 15 '17 at 06:50
  • @MihaiFlorea That's because you have other problems with that function call. See e.g. [this reference (with an example)](https://www.cs.colorado.edu/~main/bgi/doc/initgraph.html) to learn how to use the `initgraph` function. – Some programmer dude Mar 15 '17 at 07:20
  • Could you please answer with a fixed version of my code? It's way more easier for me to only compare the good w/ the wrong. Thus i tried setting the mode (second var in initgraph) to zero and deleting the useless gm. No improvements. – Mihai Florea Mar 15 '17 at 08:04
  • 1
    When asking for a *"fixed version of my code"*, I can't help but think: SO is about answering questions (to enable people to solve their problems), not solving their problems for them. But I might be wrong, feel free to ignore me. – domsson Mar 15 '17 at 10:35
  • Yes but when you have a person like me asking for help he will watch you like O_o ? untill you point exactly to what he did wrong and explain how to correct it, when he will idolatrise you after a satisfying "Aaaaah! So that's how to solve it? How simple! Can't believe I didn't see it at first! " – Mihai Florea Mar 15 '17 at 10:51
0

I fixed the obvious syntax errors and marked the places by comment `/* <-- FIXED */:

#include<iostream>
#include<graphics.h>
#include<conio.h>
using namespace std;
int main()
{
  int gr,xmax,ymax,r;
  int gm,gd; /* <-- FIXED */
  gd=DETECT;
  gm=0;
  initgraph(&gd, &gm, "C:/Users/MyName/Desktop/WORKFOLDER/"); /* <-- FIXED */
  gr=graphresult();
  if(gr!=grOk) cout<<"Error!";
  else {
    xmax=getmaxx();
    ymax=getmaxy();
    cout<<"Resol.: "<<xmax+1<<"x"<<ymax+1;
  }
setcolor(7);rectangle(0,0,xmax,ymax);setcolor(5);line(0,0,xmax,ymax);line(0,ymax,xmax,0);setcolor(3);
  for(r=(ymax+1)/2;r=0;r--) circle((xmax+1)/2,(ymax+1)/2,r);
  getch();
  closegraph();
  return 0;
}

Unfortunately, I cannot compile and test it. (I don't have BGI libs and don't want to install it.)

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
  • [It might be my mood speaking] Sir, you gave me the same link as the person before. I happen to be very stubborn and stupid: If noone corrects my program I won't understand a thing. Everyone on this network only provides me with lots of References which only blow up my mind even worse and makes me MORE stuck. Thanks for the help anyway but... can't do anything with it for now – Mihai Florea Mar 15 '17 at 08:59
  • @MihaiFlorea I didn't see the link before - sorry. The 3rd parameter of `initgraph()` is of type `char*`, intended to get a C string for (as I already mentioned) the driver path. A C string can be noted as `""` where text may occur between the double quotes. `' '` is not a C string but a character constant. It is not allowed to apply the address operator `&` on it. For me, it seems, you have problems to understand the C language itself (specifically characters, and C strings). Thus, you might investigate into this direction. (This has actually nothing to do with the graphics thing.) – Scheff's Cat Mar 15 '17 at 09:15
  • Well, I'll try working around with the example you provided. I'm using a phone now so that's pretty much the reason of asking for direct correction (which I seem not to receive) Understand that I bumped my head into a wall for three hours while noone is able to provide me with a 2-minute correction. We all pretend o help each other. But never mind, I'm pretty sure I'm going to patch it up myself in two more hours. – Mihai Florea Mar 15 '17 at 09:16
  • I just tried compiling this code. Error: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings] – Mihai Florea Mar 15 '17 at 10:55
  • And... "but may be you have to provide the actual file path." --- to WHAT file? As I told earlier I heard I need some .(dot)BGI files (they are the "drivers") for the DriverPath. Googled for 20 minutes, no download links or so.. – Mihai Florea Mar 15 '17 at 10:59
  • @MihaiFlora I used BGI library when I was a student (1990 - 1996). So, this code is _really_ old. I remember that Borland shipped it's compiler with some ".bgi" files for the common graphics boards at that time. – Scheff's Cat Mar 15 '17 at 11:03
  • I managed to SOLVE quite of the(/my) problem (i'll post the complete fixed program later for other people to see). The only error is the deprecated conversion from string constant to 'char*' [-Wwrite-strings] . It's weird - both my code and the one you put have problem with the third component/var of initgraph - the text part ... – Mihai Florea Mar 15 '17 at 11:07
  • Got egavga.bgi . Same error... (I've put the directoory(adress) of my workfolder where I have it) – Mihai Florea Mar 15 '17 at 11:12
  • @MihaiFlorea OK. This sounds like progress. Please, update your question with the exact code you have now. And, please, copy the error output text to question, also. – Scheff's Cat Mar 15 '17 at 11:15
  • Major modifications made! Please read the question and details again. – Mihai Florea Mar 15 '17 at 11:36
  • I saw your updated answer! Thanks for the corrections. I'll give it a try (on sunday) – Mihai Florea Mar 18 '17 at 05:51
  • Corrected version warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings] . It runs, but doesn't do anything, just like before. – Mihai Florea Mar 19 '17 at 15:49
  • @MihaiFlorea Actually, your graphics library should provide suitable bgi files. (A `find` in the resp. directory may help to show them.) May be, the downloaded `egavga.bgi` does not match but disturbs. Rename/move it out of the way and try again. – Scheff's Cat Mar 19 '17 at 16:01
  • @MihaiFlorea I googled again and found this: [How to setup winBGIm library in codeblock](http://www.codeincodeblock.com/2011/09/how-to-setup-winbgim-library-in.html). May be this helps. – Scheff's Cat Mar 19 '17 at 16:11
0

Just read your edited question and want to mention about LValue:

"LValue" means something which has an address i.e. you may apply the address operator &. LValue because it may appear on Left side of an assignment.

A variable has an address and thus is an LValue.

A character constant (e.g. 'A') has actually type int and is not an LValue.

A string constant (e.g. "Hello") has type const char[] (compatible to const char*) is (may be surprisingly) an LValue because the compiler must allocate data storage for it somewhere.

Thus, even the following would "work":

#include <stdio.h>
int main()
{
  char *sz = "Hello";
  printf("address of \"%s\": %p\n", sz, sz);
  printf("address of sz: %p\n", &sz);
  sz[0] = 'h';
  printf(sz);
  return 0;
}

I compiled and tested with gcc and cygwin (on Windows 10 - 64 bit):

$ gcc -o test-cstringlvalue test-cstringlvalue.c 

$ ./test-cstringlvalue.exe
address of "Hello": 0x403060
address of sz: 0x61cc6c
Segmentation fault (core dumped)

So you see two important things:

  1. It compiles fine. (Changing an LValue is not suspected by the compiler.)

  2. It crashs because a constant string has been modified.

(It probably will not crash on any platform.)

However, I recommend not to modify string constants but it demonstrates what I tried to explain about LValues.

I hope the sample does not confuse you any more. I warmly recommend to get a good C book. There are a lot of tricks possible in C but the language itself is tricky.

Edit:

Reading my own text again I felt a little bit uncertain if I told it exactly right: Thus I googled again and found this link: SO: Why are string literals l-value while all other literals are r-value?.

Community
  • 1
  • 1
Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
  • And how could this help me fix the program? – Mihai Florea Mar 15 '17 at 10:48
  • @MihaiFlorea Is your problem still the one stated in your question? '"Why isn't the program compiled successfully?" In this case, Some programmer dude already gives the answer. It is also found in the sample code I provided in my 1st answer. I just tried to elaborate the background a little bit in my 2nd answer. – Scheff's Cat Mar 15 '17 at 10:59
  • Major modifications made! Please read the question and details again. – Mihai Florea Mar 15 '17 at 11:35
0

Solved! Downgraded CB (v16 to v13 I think, not sure if it was necessary) and replaced some files. I followed this tutorial: https://m.youtube.com/watch?v=FxCdMM9H66I It was quite the same as the link in the question but probably (one or more of) those files (graphics.h, winbgim.h, libbgi.a) from the previous link were bugged, had some unobvious errors or so, or were incompatible etcetera), while the files given by the maker of the vid I provided in this answer were corrected. I don't (really want to) know. Now, the warning still appears, BUT the program runs perfectly! I replaced r=0 with r>=0 , this was a minor mistake. (I'll update the question if I still can)

Thanks for... your help! (Can't say "nothing", I'd be mean) :)

EDIT:

Some "cleaner" version that shows the circle (just realised the variant in the question doesn't display the filled circle):

#include<iostream>
#include<graphics.h>
#include<dos.h>
using namespace std;
main(){
int gm,gd,gr,xmax,ymax,r;
gd=DETECT;
gm=0;
initgraph(&gd,&gm,"C:\\TC\\BGI");
gr=graphresult();
if(gr!=grOk) cout<<"Error!";
    else    {xmax=getmaxx();
            ymax=getmaxy();
            cout<<"Res.: "<<xmax+1<<"x"<<ymax+1;}
setcolor(7);
rectangle(0,0,xmax,ymax);
setcolor(5);
line(0,0,xmax,ymax);
line(0,ymax,xmax,0);
setcolor(3);
for(r=(ymax+1)/2;r>=0;r--) circle((xmax+1)/2,(ymax+1)/2,r);
getch();
closegraph();
}

EDIT2: Downgrading not necessary CONFIRMED! :D

Mihai Florea
  • 63
  • 1
  • 2
  • 8
0

The warning should go away if you replace the literal with an array that can be converted to char*:

char driver[] = "";
initgraph(&gd, &gm, driver);
jack_1729
  • 382
  • 1
  • 3
  • 14