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++
  
User name:
Password:
Reply:
 

UserPost
Ramsus
Posts: 103/162
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.
beneficii
Posts: 134/567
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++.
neotransotaku
Posts: 3213/4016
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
paraplayer
Posts: 83/280
i thought C++ had everything C had...
sloat
Posts: 56/85
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.
beneficii
Posts: 133/567
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.


Ramsus
Posts: 102/162
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
Posts: 132/567
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
Posts: 101/162
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.
beneficii
Posts: 131/567
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
Posts: 100/162
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.

interdpth
Posts: 473/527
Usually when I see a convert error I just cast it like (int*) variable/pointer/address
beneficii
Posts: 130/567
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!
sloat
Posts: 56/85
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.
Ramsus
Posts: 99/162
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?
neotransotaku
Posts: 3200/4016
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.
Banedon
Posts: 1245/1408
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;
};
beneficii
Posts: 129/567
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.
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.015 seconds.