C/C++ Thread

Everyone has their favorite language which they use most of the time.
And which one they will use to start learning this and which one they will use later is completely up to them.

For example I started with old VB,but that does not mean I should still use it now lol.Now I use mostly C# and C++ here and there if I decide to translate my C# applications to C++.

Like I said...it completely depends on the person what language he/she will use.The goal is the same: MAKE GOOD SOFTWARE AND SELL IT!:D
People who buy your software do not ask in what language it was made in lol:D




Cheers!
 
Now I use mostly C# and C++ here and there if I decide to translate my C# applications to C++.

Wait, why would you want to translate your applications? Surely once they are compiled anyone can run them anyway? Or would you only do it if you wanted to work with another programmer but they prefer C++??

Also, is it easy to translate, say, C applications into C# or C++?
 
Wait, why would you want to translate your applications? Surely once they are compiled anyone can run them anyway? Or would you only do it if you wanted to work with another programmer but they prefer C++??

Also, is it easy to translate, say, C applications into C# or C++?

LoL don't worry.I do not translate applications from C# to C++ or from C++ to C# very often anyway.I do that only if a team prefers that language or if I am bored as hell like I am right now lol because my main computer on which I am programming is scanning right now as I am writing this and it will take approximately 6 hours or more to finish so I have nothing better to do anyway lol xD

As for your question: Is it EASY to convert C into C# or C++...
Well that term EASY really depends on your knowledge lol.For me it's not hard,but for someone who just started learning this it is going to be mission impossible lol.That is why all beginners should use VB.It's short,simple and easy to understand :P

Unforcenately there is no really a C to C# converter which will do ALL the job for you.

For example if you want to translate C application into C# application,the best way is to do it like this:

-Copy the C code into the C++ compiler such as Visual C++ IDE...
-Compile the application and then correct the errors that occured.And believe me,there WILL be errors lol.There always are xD...
-Add/remove/change some programming code as neccessary...
-Compile again and make sure there are no syntax,runtime and logical errors...
-Test the application to make sure it is working properly...
-Now once the application fully works as 100% C++ application,you can turn it into a C# application on few ways:

--->Use Salamander .NET converter to convert the C++ code into the C# code and then copy that converted code into the C# compiler such as Visual C# IDE,compile it and make sure it's working properly.If there are errors of any kind,correct them...

--->Use Spices .NET converter to convert the C++ code into the C# code and then copy that converted code into the C# compiler such as Visual C# IDE,compile it and make sure it's working properly.If there are errors of any kind,correct them...

--->Or the hardest way is to copy the C++ code into the C# compiler such as Visual C# IDE directly and rechange and correct the entire code manually which is pain in the ass lol.This is how I do it.It's not really that hard if you know how to do it,but you will lose a lot of time believe me xD
And that is because each method,string,int,bool values,brackets,points,events,controls,fields,references and all other things needs to be rewritten manually to make them to work as a C# application.It might seem to be a lot of work,but for example if you need to rewrite the SAME thing on many places such as:

Application::Exit();

to

Application.Exit();

or

this->Text="This sucks!";

to

this.Text="This sucks!";


Then in that case you can always use the REPLACE ALL function in IDE to save some little time instead rewriting all those same things manually.Still if the application is big and has many lines of code,in most cases REPLACE ALL will not be enough lol and you will need to do many changes manually.That is why there are C++ to C# converters.Because they do most of the job for you so it saves you a lot of torture lol.

Still I prefer to do it manually.I guess I am just used to do it that way and at least I am 100% sure that I did not miss anything.:D

For example my UAC keyboard spy version 1.0 project on which I am working right now has approximately 700 pages (plus approximately 200 more pages which I need to finish this month) full of C# code and there are a LOT LOT LOT different things inside.So now just imaginate how big torture it would be to manually translate all that into C++ lol.It would be a HUGE pain in the ass believe me:D
Same applies for C++ into C#.It is huge torture on both ways believe me lol.

Yes it's true that once applications are compiled in one language then anyone can run them,but like I said...I translate them to kill the boring time I am having right now lol.Sometimes it's cool to have software you made in two languages :P They will both of course work the same,but it's cool lol...I am crazy I know :D




Cheers!
 
Wowee, that was a long reply!!

I'm definitely a long way from writing whole, useful, applications in any language, let alone translating them, but from all that you said, it sounds like it would almost be easier to just rewrite the whole thing?

Our lecturer had us save our source file as .cpp in one exercise to show that it wouldn't compile, and I figure that's because there a basic difference in how you start a C++ program... Like are the libraries specified differently? Or is the function (the main () bit...) done differently?
 
There are a few differences in the language, the only two I can think of right now are C allows implicit casting of pointers and C requires an instance of a struct to be declared as "struct Typename instancename" instead of "Typename instancename" that's done in C++ (though in C typedefs are generally used to replicate this behaviour). There are others too, but most of them are trivial - however, it does mean that you can't always just convert straight C into C++ without making some changes even though C++ is mostly backwards compatible with C.

As for rewriting stuff that you already have in C, you can use C source files in C++ projects easily and usually there are no difficulties - as long as the compiler knows that they're C files rather than C++ and there are no name collisions you should be fine. Using C code in C# (or any managed language like others in the .NET family or Java for that matter) is trickier and you'll either have to rewrite or use something like JNI (no idea if .NET has an equivalent, though I would imagine it does).

I'll also point out that despite its name, C# is nothing like C save for basic constructs that exist in nearly all languages and curly braces. I'd say it's closer to Java than either C or C++.
 
I'm definitely a long way from writing whole, useful, applications in any language, let alone translating them, but from all that you said, it sounds like it would almost be easier to just rewrite the whole thing?

Depends on how big your program is.If it has let's say 3 pages then it is of course easier to write it whole in C++,but if it has let's say 700 pages then it is torture on both ways,but it is still easier to just copy and paste the code and fix bigs manually.It WILL take a long time,but it will be definetely shorter than writing the whole thing manually again believe me;)
 
Hey guys I started a project in C language after finishing C# projects. I'm using visual studio 2008 for this C project. My question is how to debug this C language in this software? When I put a breakpoint and try running the codestep by step then it goes through hundreds of lines of codes that included .h and function definition files. I just want to debug my .C file. Can some one tell me how?
 
Try using step over unless the function call you are on is something you have written then step into is ok.
 
Can anyone tell me which is the best book for x86 assembly?
I have lots of projects on it in college. Can anyone tell me good book which starts from basics and advance. :)
 
How do I properly pass an array and change it's value in C?

I have this. It gives me a segfault. I'm trying to get it to scan integers from the file to the array that's declared in main.

Code:
int main() {
    int v[NUM_ELEMENTS];
    int n; 
    int min;
    int max;
    double mean;
       
    getNums(v, &n);
    calcMin (v, n, &min);
    calcMax (v, n, &max);
    calcMean (v, n, &mean);
    return 0;
}

void getNums(int v, int *n) {
    FILE *datafile;
    datafile = fopen (FNAME, "r");
    int count = 0;
    while (!feof (datafile)) {
        int next;
        fscanf (datafile, "%d", next);
        v = next;
        count++;
    }
    *n = count;
    printf ("%d\n", count);
}

NOTE: I'm required to use fscanf as well as declaring all my variables in main.
 
Last edited:
Firstly, you're passing an int rather than a pointer to an int which is what I assume you want (&next instead of just next), that's probably what's causing the segfault. I also don't see what you're using v in getNums for - you're assigning it values without ever reading it.
 
I just posted the code that actually compiled without error. I know it's not right. Here is exactly what is suppose to happen in getNums:

send v and n (by reference) to getNums
Create a file pointer and open a file for reading (contains a list of ints)
While not at the end of the file:
-----Read an integer from the file into the v array element at position count
-----update count
set n equal to count


So essentially, I have to pass in an array and an int and change their values.
 
I just posted the code that actually compiled without error. I know it's not right.

So essentially, I have to pass in an array and an int and change their values.

Firstly, you're passing an int rather than a pointer to an int which is what I assume you want (&next instead of just next), that's probably what's causing the segfault. I also don't see what you're using v in getNums for - you're assigning it values without ever reading it.

Hackapelite answered your question :P.

PHP:
#include <iostream>
using namespace std;

//pass in the array with *
//pass by reference with &
void function(int *my_array, int &my_value)
{
	my_array[0] = 10; //access the array
	my_array[3] = 2; //access the array
	
	my_value = 6; //access the variable
}

int main()
{
	int v[10]; //make array
	int x; //make variable
	function (v, x); 

	cout << v[0] << ", " << v[3] << endl; //outputs 10, 2
	cout << x << endl; //outputs 6
}
 
Hackapelite answered your question :P.

He didn't really, though. The next variable is set to the value read by fscanf. Then next is added to the array as an element. I don't see why I need a pointer for the next variable.

And The only thing getNums is suppose to do is put values into the array, as well as count the number of elements that are put in.

Unless I misunderstood. And it's your examples like yours that is throwing me off. Every time I look at a different example for the same thing, it always looks different. Everyone puts their asterisks and ampersands in different places. I formatted mine just like you did, and it didn't work. Are C++ pointers the same as C?
 
He didn't really, though. The next variable is set to the value read by fscanf. Then next is added to the array as an element. I don't see why I need a pointer for the next variable.

And The only thing getNums is suppose to do is put values into the array, as well as count the number of elements that are put in.

Unless I misunderstood. And it's your examples like yours that is throwing me off. Every time I look at a different example for the same thing, it always looks different. Everyone puts their asterisks and ampersands in different places. I formatted mine just like you did, and it didn't work. Are C++ pointers the same as C?

Oh man, I'm sorry, I didn't realize you were doing this in C. And I do admit, everyone uses pointers (* and &) differently, which is confusing.

In which case my corrected code would be,
PHP:
#include <stdio.h>

void function(int *my_array, int *my_value)
{
    my_array[0] = 10; //access the array
    my_array[3] = 2; //access the array
    
    *my_value = 6; //access the variable
}

int main()
{
    int v[10]; //make array
    int x; //make variable
    function (v, &x); 

	printf("%i", v[0]); //prints 10
	printf("%i", x); //prints 6.
}

Anyway, you need to walk through your code better to understand why its not working.

PHP:
//you are saying pass in an int, and a pointer to an int.
void getNums(int v, int *n) {

//so right now, v is a single integer (its actually a copy) and n is a pointer to an integer.
//this v will be destroyed at the end of this method.

    FILE *datafile; 
    datafile = fopen (FNAME, "r");
    int count = 0;

    while (!feof (datafile)) {
        int next;
        fscanf (datafile, "%d", next); //you're only passing by value here
        v = next; //you're setting the single v integer over and over again to the next value
        count++;
    }
    *n = count; //this line is correct.
    printf ("%d\n", count);
}

I have not tested the below, but this is probably what you're wanting.

PHP:
int main() {
    int v[NUM_ELEMENTS];
    int n; 
    int min;
    int max;
    double mean;
       
    getNums(v, &n);
    calcMin (v, n, &min);
    calcMax (v, n, &max);
    calcMean (v, n, &mean);
    return 0;
}

//pass a pointer to v and n.
void getNums(int *v, int *n) {

//so at this point v and n are both pointers.
//great thing about c (and confusing as heck) pointers can act as arrays
//this gets really stupid confusing when you find out char and c strings are also just (null terminated) pointers thus arrays as well...

    FILE *datafile;
    datafile = fopen (FNAME, "r");
    int count = 0;
    while (!feof (datafile)) {
        int next;
        fscanf (datafile, "%d", &next); //you want to pass the address here, not the value. 

        v[count] = next; //this sets the value inside the array
        count++;
    }
    *n = count;
    printf ("%d\n", count);
}
 
Last edited:
Oh man, I'm sorry, I didn't realize you were doing this in C. And I do admit, everyone uses pointers (* and &) differently, which is confusing.

In which case my corrected code would be,
PHP:
#include <stdio.h>

void function(int *my_array, int *my_value)
{
    my_array[0] = 10; //access the array
    my_array[3] = 2; //access the array
    
    *my_value = 6; //access the variable
}

int main()
{
    int v[10]; //make array
    int x; //make variable
    function (v, &x); 

	printf("%i", v[0]); //prints 10
	printf("%i", x); //prints 6.
}

Anyway, you need to walk through your code better to understand why its not working.

PHP:
//you are saying pass in an int, and a pointer to an int.
void getNums(int v, int *n) {

//so right now, v is a single integer (its actually a copy) and n is a pointer to an integer.
//this v will be destroyed at the end of this method.

    FILE *datafile; 
    datafile = fopen (FNAME, "r");
    int count = 0;

    while (!feof (datafile)) {
        int next;
        fscanf (datafile, "%d", next); //you're only passing by value here
        v = next; //you're setting the single v integer over and over again to the next value
        count++;
    }
    *n = count; //this line is correct.
    printf ("%d\n", count);
}

I have not tested the below, but this is probably what you're wanting.

PHP:
int main() {
    int v[NUM_ELEMENTS];
    int n; 
    int min;
    int max;
    double mean;
       
    getNums(v, &n);
    calcMin (v, n, &min);
    calcMax (v, n, &max);
    calcMean (v, n, &mean);
    return 0;
}

//pass a pointer to v and n.
void getNums(int *v, int *n) {

//so at this point v and n are both pointers.
//great thing about c (and confusing as heck) pointers can act as arrays
//this gets really stupid confusing when you find out char and c strings are also just (null terminated) pointers thus arrays as well...

    FILE *datafile;
    datafile = fopen (FNAME, "r");
    int count = 0;
    while (!feof (datafile)) {
        int next;
        fscanf (datafile, "%d", &next); //you want to pass the address here, not the value. 

        v[count] = next; //this sets the value inside the array
        count++;
    }
    *n = count;
    printf ("%d\n", count);
}

Hahaha just mailed him the same code about 4 minutes before your reply, except it has a count--; :D
 
Haha. Yeah. He did end up doing that. Sorry if I sounded like an ass. I didn't mean to. I really do appreciate the help.
 
He didn't really, though. The next variable is set to the value read by fscanf. Then next is added to the array as an element. I don't see why I need a pointer for the next variable.
fscanf takes pointers to values, not values itself since otherwise it has no way of setting the values (C doesn't have C++ style references). You're passing the function an unitialised int, not it address, but since fscanf expects a pointer it dereferences the garbage value anyway which may point to any arbitrary memory location (possibly NULL if your compiler initialises primitives by default).

EDIT: Crap, just read the whole thread again, I assume the problem has been solved?
 
Last edited:
fscanf takes pointers to values, not values itself since otherwise it has no way of setting the values (C doesn't have C++ style references). You're passing the function an unitialised int, not it address, but since fscanf expects a pointer it dereferences the garbage value anyway which may point to any arbitrary memory location (possibly NULL if your compiler initialises primitives by default).

EDIT: Crap, just read the whole thread again, I assume the problem has been solved?

Yeah it is. Thank you for explaining that, though. Even though I got it fixed, I didn't understand the solution.
 
Back
Top