Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - An Issue With Converting Code From C to C++ | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
beneficii

Lakitu
Level: 36

Posts: 129/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 06-14-05 04:56 PM Link | Quote
In a C Program, I saw a piece of code like this:

typedef struct {
int num_ptrs;
int offset;
} MAPSCREEN_POINTER_LOCS;

MAPSCREEN_POINTER_LOCS pointer_tables[9] = {
{21, 0x19438}, // World 1 map pointers
{47, 0x194BA}, // World 2 map pointers
{52, 0x195D8}, // World 3 map pointers
{34, 0x19714}, // World 4 map pointers
{42, 0x197E4}, // World 5 map pointers
{57, 0x198E4}, // World 6 map pointers
{46, 0x19A3E}, // World 7 map pointers
{41, 0x19B56}, // World 8 map pointers
{10, 0x19C50}, // Warp Zone pointers (no enemy set / level pointers)
};

When the variable pointer_tables was declared as a struct array, each item of the struct could be declared within brackets. This program worked in C.

In C++, however, I get errors like "cannot convert from const int to 'MAPSCREEN_POINTER_LOCS'" (and this refers to each of the numbers declared in the struct array. I made the structure like

struct MAPSCREEN_POINTER_LOCS {
" " "
};

but to no avail. I did read that C++ tends to be stricter about this sort of thing than C, but does C++ allow you to declare struct arrays this way, or am I simply doing something wrong? Thanks.
Banedon

Giant Red Paratroopa
Level: 55

Posts: 1245/1408
EXP: 1291380
For next: 22809

Since: 03-15-04
From: Michigan

Since last post: 101 days
Last activity: 90 days
Posted on 06-14-05 07:55 PM Link | Quote
I know that in C++, you don't need to do the whole "typedef struct" thing at all...for instance, let's say you had the following struct:

struct person
{
int age;
int gender;
int numberOfEyeballs;
};

In C you would have to do the following to create a variable of type struct person:

struct person me;

but in C++ you use:

person me;

The typedef trick in C is to get around having to use the word "struct" when declaring a variable that is of a struct type. Since this isn't needed in C++, you should be able to redefine the struct as follows:

struct MAPSCREEN_POINTER_LOCS
{
int num_ptrs;
int offset;
};


(edited by Banedon on 06-14-05 10:55 AM)
neotransotaku

Baby Mario
戻れたら、
誰も気が付く
Level: 87

Posts: 3200/4016
EXP: 6220548
For next: 172226

Since: 03-15-04
From: Outside of Time/Space

Since last post: 11 hours
Last activity: 1 hour
Posted on 06-14-05 09:46 PM Link | Quote
I don't believe C++ will permit this. However, at a lower level, it is exactly the same thing. So, a work around you can try doing is to just declare the array space for that. Cast the pointer to be an int pointer and then write the values in that way.
Ramsus

Octoballoon
Level: 19

Posts: 99/162
EXP: 34651
For next: 1126

Since: 01-24-05
From: United States

Since last post: 39 days
Last activity: 71 days
Posted on 06-14-05 11:25 PM Link | Quote
sloat: What the hell? The original code is just fine. You just made it longer, and I don't think you realize how it's being used. Also, what's the point of creating a pointer to the address of the first element of an array? THAT'S WHAT AN ARRAY IS.

To the original post:

It should be perfectly valid C++, and it does work perfectly with GCC 4. The typedef shouldn't make a difference either, since that just creates a new name for the same type (as opposed to a different type).

My only suggestion, aside from removing the unnecessary typedef, is to remove the [9] and replace it with []:


MAPSCREEN_POINTER_LOCS pointer_tables[] = {


What compiler are you using?



(edited by Ramsus on 06-14-05 02:28 PM)
(edited by Ramsus on 06-14-05 02:28 PM)
(edited by Ramsus on 06-14-05 02:29 PM)
sloat

Level: 16

Posts: 56/85
EXP: 18044
For next: 2212

Since: 05-21-04
From: South Central Delaware

Since last post: 19 days
Last activity: 5 hours
Posted on 06-15-05 12:17 AM Link | Quote
Originally posted by Ramsus
sloat: What the hell?



Hello to you too. Nice to meet you.


The original code is just fine. You just made it longer, and I don't think you realize how it's being used.



I just figured that since he had errors compiling it, he might want to try a different way.

And it is longer. Good observation.


Also, what's the point of creating a pointer to the address of the first element of an array?



I was just demonstrating that you can have a pointer type of the struct with the struct's definition and how it could potentially be used. It didn't have to be the first element, but 0 was what I chose.


THAT'S WHAT AN ARRAY IS.



What's the point in getting mad about it?
If it bothers you that much, I'll remove the code from my old post. No skin off my sac.

Thanks for the input.
beneficii

Lakitu
Level: 36

Posts: 130/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 06-15-05 02:18 AM Link | Quote
EDIT: It didn't let me show the include files, probably because of the arrows. Let me try a different way.

Originally posted by Ramsus
sloat: What the hell? The original code is just fine. You just made it longer, and I don't think you realize how it's being used. Also, what's the point of creating a pointer to the address of the first element of an array? THAT'S WHAT AN ARRAY IS.

To the original post:

It should be perfectly valid C++, and it does work perfectly with GCC 4. The typedef shouldn't make a difference either, since that just creates a new name for the same type (as opposed to a different type).

My only suggestion, aside from removing the unnecessary typedef, is to remove the [9] and replace it with []:


MAPSCREEN_POINTER_LOCS pointer_tables[] = {


What compiler are you using?



Using MSVC++. I did another test, first with a C program version then with a C++ program version and used MSVC++ to compile it. Here's the source code for the C program, which worked fine (excuse the language btw, it's used to help express my anger at this):

#include <stdio.h>

void main(void) {
struct BITCH {
char *punks;
const int hos;
};

struct BITCH slapya[] = {
{"FUCK", 2},
{"YEAH", 3},
{"SHIT", 4},
{"DAMN", 5},
};

printf("Fuck you!\n", "");
}

Then I made it into C++, which was:

#include <iostream>

void main(void) {
struct BITCH {
char *punks;
const int hos;
};

BITCH slapya[] = {
{"FUCK", 2},
{"YEAH", 3},
{"SHIT", 4},
{"DAMN", 5},
};

cout << "Fuck you!\n";
}

It came out with these errors:

E:\Program Files\Microsoft Visual C++\MyProjects\win\mainw.cpp(10) : error C2440: 'initializing' : cannot convert from 'char [5]' to 'struct main::BITCH'
No constructor could take the source type, or constructor overload resolution was ambiguous
E:\Program Files\Microsoft Visual C++\MyProjects\win\mainw.cpp(10) : error C2440: 'initializing' : cannot convert from 'const int' to 'struct main::BITCH'
No constructor could take the source type, or constructor overload resolution was ambiguous
E:\Program Files\Microsoft Visual C++\MyProjects\win\mainw.cpp(10) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.

I'm downloading GCC btw. I'm gonna try it out there. Thanks for the tip!


(edited by beneficii on 06-14-05 05:21 PM)
interdpth

Rex
Level: 36

Posts: 473/527
EXP: 294398
For next: 13712

Since: 03-20-04

Since last post: 10 days
Last activity: 31 days
Posted on 06-15-05 02:19 AM Link | Quote
Usually when I see a convert error I just cast it like (int*) variable/pointer/address
Ramsus

Octoballoon
Level: 19

Posts: 100/162
EXP: 34651
For next: 1126

Since: 01-24-05
From: United States

Since last post: 39 days
Last activity: 71 days
Posted on 06-15-05 02:46 AM Link | Quote
Originally posted by sloat
Originally posted by Ramsus
sloat: What the hell?



Hello to you too. Nice to meet you.


The original code is just fine. You just made it longer, and I don't think you realize how it's being used.



I just figured that since he had errors compiling it, he might want to try a different way.

And it is longer. Good observation.


Also, what's the point of creating a pointer to the address of the first element of an array?



I was just demonstrating that you can have a pointer type of the struct with the struct's definition and how it could potentially be used. It didn't have to be the first element, but 0 was what I chose.


THAT'S WHAT AN ARRAY IS.



What's the point in getting mad about it?
If it bothers you that much, I'll remove the code from my old post. No skin off my sac.

Thanks for the input.


Sorry for offending, but your code doesn't really take on the problem of initializing the array of structs to begin with. It merely provides another method of accessing the members.

beneficii

Lakitu
Level: 36

Posts: 131/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 06-15-05 02:52 AM Link | Quote
Originally posted by Ramsus
Originally posted by sloat
Originally posted by Ramsus
sloat: What the hell?



Hello to you too. Nice to meet you.


The original code is just fine. You just made it longer, and I don't think you realize how it's being used.



I just figured that since he had errors compiling it, he might want to try a different way.

And it is longer. Good observation.


Also, what's the point of creating a pointer to the address of the first element of an array?



I was just demonstrating that you can have a pointer type of the struct with the struct's definition and how it could potentially be used. It didn't have to be the first element, but 0 was what I chose.


THAT'S WHAT AN ARRAY IS.



What's the point in getting mad about it?
If it bothers you that much, I'll remove the code from my old post. No skin off my sac.

Thanks for the input.


Sorry for offending, but your code doesn't really take on the problem of initializing the array of structs to begin with. It merely provides another method of accessing the members.




What method did he use? I think he deleted it, but I'd be itnerested in hearing about it.
Ramsus

Octoballoon
Level: 19

Posts: 101/162
EXP: 34651
For next: 1126

Since: 01-24-05
From: United States

Since last post: 39 days
Last activity: 71 days
Posted on 06-15-05 03:01 AM Link | Quote
He created typedefs for the struct and a pointer to the struct, then he initialized an array of the structs as you did, then he create a pointer to an element of the array (i.e. *sp = &array[k]; ).

i.e.

typedef struct _s {
int a;
int b;
} S, *SP;

S s[] = {...}; whatever
SP sp = &s[0];

To the compiler, it should end up being exactly the same (typedef doesn't really create a new type), but I might have misread his code, in which case I wish he hadn't deleted it so I could correct myself.

EDIT:
Anyway, I just got ticked off by the "I can't believe you got away with C" comment, not the code itself.

Are you using Visual C++ 6? If so, I'd also suggest getting a new version of Visual Studio, since VC++ 6 doesn't really support the new ISO standard.


(edited by Ramsus on 06-14-05 06:02 PM)
(edited by Ramsus on 06-14-05 06:07 PM)
beneficii

Lakitu
Level: 36

Posts: 132/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 06-15-05 03:28 AM Link | Quote
Ramsus,

Yeah, I think that's a good idea, but it costs money. I just download GCC 4. I'm now hoping to put it to use. I also downloaded G++, which I guess allows you to use C++ as well. BTW, I can't seem to find a windows.h file anywhere in the two archives. This allows Windows programming, right?
Ramsus

Octoballoon
Level: 19

Posts: 102/162
EXP: 34651
For next: 1126

Since: 01-24-05
From: United States

Since last post: 39 days
Last activity: 71 days
Posted on 06-15-05 03:34 AM Link | Quote
GCC (all caps means the collection of gcc, g++, g77, etc. compilers for various languages) has a native Windows port maintained by the MinGW project. Check out their homepage here:

http://www.mingw.org/

You just need the MinGW-version.exe file to install the compiler tools, but they're commandline only. If that's fine, you might also download MSYS, which provides a bash shell and the make command (which you'll pretty much need for managing code without an IDE).

However, DevC++ is a free IDE that comes with MinGW, so you can just go to:

http://www.bloodshed.net/dev/devcpp.html

And download/install DevC++ instead. If you've never written a Makefile and don't have a good editor (i.e. VIM or EMACS), I'd suggest just using DevC++.

Good luck.

beneficii

Lakitu
Level: 36

Posts: 133/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 06-15-05 06:05 AM Link | Quote
I remember this program, DevC++. It doesn't seem as buggy as it used ot be. Thanks for pointing it out again! For one thing, I can declare struct arrays like I want and have it be C++!

Originally posted by Ramsus
GCC (all caps means the collection of gcc, g++, g77, etc. compilers for various languages) has a native Windows port maintained by the MinGW project. Check out their homepage here:

http://www.mingw.org/

You just need the MinGW-version.exe file to install the compiler tools, but they're commandline only. If that's fine, you might also download MSYS, which provides a bash shell and the make command (which you'll pretty much need for managing code without an IDE).

However, DevC++ is a free IDE that comes with MinGW, so you can just go to:

http://www.bloodshed.net/dev/devcpp.html

And download/install DevC++ instead. If you've never written a Makefile and don't have a good editor (i.e. VIM or EMACS), I'd suggest just using DevC++.

Good luck.


sloat

Level: 16

Posts: 56/85
EXP: 18044
For next: 2212

Since: 05-21-04
From: South Central Delaware

Since last post: 19 days
Last activity: 5 hours
Posted on 06-15-05 07:43 AM Link | Quote
Originally posted by Ramsus
Anyway, I just got ticked off by the "I can't believe you got away with C" comment, not the code itself.



I can't remember if that's what I said or not. But what I meant to say was "I can't believe you got away with that code in C." Turns out I misread the original post (I thought that he had wrote the code originally), but I was still suprised that using "typedef struct" without naming the struct would compile.

Sorry about the deletion/overreaction.
paraplayer

Snifit
Level: 22

Posts: 83/280
EXP: 57271
For next: 1079

Since: 06-06-05

Since last post: 44 days
Last activity: 44 days
Posted on 06-15-05 09:12 AM Link | Quote
i thought C++ had everything C had...
neotransotaku

Baby Mario
戻れたら、
誰も気が付く
Level: 87

Posts: 3213/4016
EXP: 6220548
For next: 172226

Since: 03-15-04
From: Outside of Time/Space

Since last post: 11 hours
Last activity: 1 hour
Posted on 06-15-05 11:08 AM Link | Quote
C++ does have the same power that C has--it's just that certain constructs aren't valid anymore in C++ unless you work with the new rules that have been imposed
beneficii

Lakitu
Level: 36

Posts: 134/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 06-15-05 11:30 AM Link | Quote
Originally posted by neotransotaku
C++ does have the same power that C has--it's just that certain constructs aren't valid anymore in C++ unless you work with the new rules that have been imposed


It also depends on your compiler, to an extent. That thing I put in the original post can be done in GCC's version of C++, but not in MSVC++'s version of C++.
Ramsus

Octoballoon
Level: 19

Posts: 103/162
EXP: 34651
For next: 1126

Since: 01-24-05
From: United States

Since last post: 39 days
Last activity: 71 days
Posted on 06-15-05 06:48 PM Link | Quote
EDIT: Also, you might check if this Visual Express C/C++ compiler Microsoft is making works: http://lab.msdn.microsoft.com/express/visualc/default.aspx

GCC is used on most platforms these days though, since it's open source and can be ported and used much more quickly than if one wrote their own compiler. Especially important these days, since C++ is REALLY hard to implement completely.

And speaking of C vs. C++, to be perfectly honest, I usually find (like some others have mentioned) the only real problems being having to explicitly typecast in certain situations. C++ even provides 4 newer (longer) types of casts that can add more typesafety by allowing just what you want to allow, although if you're typecasting in the first place, you probably don't care.

They're worthwhile to go over though.

All the extra syntax in C++ kind of bothers me though. That's so it can be as typesafe as Ada (theoretically) and much shorter at the same time, but it still makes some things ambiguous. Not a problem if you're alert and you understand the classes in use.

Originally posted by sloat
Originally posted by Ramsus
Anyway, I just got ticked off by the "I can't believe you got away with C" comment, not the code itself.



I can't remember if that's what I said or not. But what I meant to say was "I can't believe you got away with that code in C." Turns out I misread the original post (I thought that he had wrote the code originally), but I was still suprised that using "typedef struct" without naming the struct would compile.

Sorry about the deletion/overreaction.


I actually meant to type "with that in C", but that doesn't matter because I took it to mean that the way the original code was written was somehow horribly incorrect or bad style (sort of a pompous "you should do it this way instead"). I see what you meant now though. Sorry about that.

And yes, you can have anonymous structs in C and C++, although if you declare a struct with the same members, it should be a different type. They should still be mapped the same way in memory, but that could easily change from implementation to implementation.



(edited by Ramsus on 06-15-05 09:55 AM)
(edited by Ramsus on 06-15-05 10:23 AM)
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
Acmlm's Board - I2 Archive - Programming - An Issue With Converting Code From C to C++ | |


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.014 seconds.