   Please help! C++ to C#
  hello guys,I have a little excercise where I am suppose to understand what's going on in the following code and then convert the code to c#.To be honest I am not very good with pointers, so a little confused what how exactly to convert this to c#. Especially couple of comments I've made.Of course I don't expect you people to just do the translation for me, but please point me in the right direction. I know c# doesn't use pointers, so should I use string instead of char* ?? please help.thanks!#include <iostream>using namespace std;int testing(int plvl, int nFnum, int count, int pl_parsed, int vpl_parsed, int loop);int main (){    int result = 0;    result = testing(0, 0, 0, 0, 0, 0);}int testing(int plvl, int nFnum, int count, int pl_parsed, int vpl_parsed, int loop){    char* next = "This is a test string, where I check for commas, commas END";    int comma = ',';//HOW IS THIS POSSIBLE???    float data;    char* pComma;    while (loop<6)    {   if (pl_parsed == count)   {  nFnum ++;  plvl = vpl_parsed;  vpl_parsed = 0;  pl_parsed = 0;   }   pComma = strchr( next, comma );//ESPECIALLY WHAT TO DO WITH THIS!!!   if (pComma != NULL)   {  *pComma = '\0';//WHAT IS THIS FOR??  if(strcmp(next, "END"))  { data = atof(next); vpl_parsed ++;  }  next = pComma + 1;//HOW CAN YOU ADD 1 to char??  pl_parsed ++;   }   if(strcmp(next, "END"))//HOW CAN I DO THIS???   {  data = atof(next);  vpl_parsed++;   }   loop++;    }    return nFnum + plvl;}[Edited by - musafir2007 on January 24, 2010 10:51:37 PM]
  btw, just realized.. for some reason*pComma = '\0';gives me runtime error.. any idea why??   ;  That code is awful. Where did you get it from?Quote:btw, just realized.. for some reason*pComma = '\0';gives me runtime error.. any idea why??Because pComma points at a comma somewhere inside the test string, so this assignment tries to replace the comma with a null terminator. But you can't replace anything inside the test string because it's a string literal, i.e. a constant value set before the program runs.   ;  hmm.. i see. how can you make test string not constant then?and i guess this code is suppose to be confusing.. this is suppose to help us grasp the concepts better.thank your for your answer but can you please also answer couple of other questions I had like how to switch from char* to c#?   ;  I'm not extremely familiar with c#, but I am very familiar with c++.First a basic review of pointers: pointer | V"This is a string"The pointer is like the location of the character 'i'when you add 1 to the pointer, you are shifting it to the right one character so it would point to the 's'All character arrays need to be terminated with a NULL character, aka \0 or NULL or 0x00So if you find when the pointer is pointing to a null character, you just see if *pointer == NULLThe * is saying what does the pointer point to.  The pointer is the location, and the * operator extracts the content at that location.I'm pretty sure you can still use pointers in c# if you want, but strings are often more convenient.For your comments:int comma = ',';the ',' is the ascii representation for ,pComma = strchr( next, comma );//ESPECIALLY WHAT TO DO WITH THIS!!!http://www.cplusplus.com/reference/string/string/find/for this just use the find function from the string class, it will give you the index of its location.  With this method you can change what you do later in the code.  It seems like you are trying to find a comma and then end the original string there. (\0 sets the string to end there)next = pComma + 1;//HOW CAN YOU ADD 1 to char??It is moving the pointer to the next character.if(strcmp(next, "END"))//HOW CAN I DO THIS???with strings you can just say:if( thestring == "END" )Hope that helps   ;  I agree with Zahlman, the C++ code you have is terrible!If you want to replace characters in the middle of a string, use a StringBuilder.Any code that appears to modify a string when not using a StringBuilder actually creates a new string with your changes.   ;  Let's get some background information here.Why do you have this exercise?Were you told what the code is supposed to do?   ;  C# uses pointers extensively. They're more strict though, you can't just say "I wanna point at that address and write a bunch of shit in". They're "managed".But that's not relevant. You wouldn't just translate this code over. You'd rewrite based on what its supposed to DO.C#'s standard library has tons of string processing features.Do you understand what this program DOES? Cause if you do, you can pretty much stop looking at it. The implementation in C#.NET is going to be radically different.EDIT: Looking over that C++, I have no idea what the shaq it does. What DOES it do?   ;  Hi, thank you everyone for your replies. Actually this just an extra assignment that our teacher gave saying that understanding and translating it into c# should be good excercise. But I guess teh code itself is crapy.1 question though.. in c# can I use an array of char instead of string to accomplish this.. c[] would that be easier?thanks;  You can grab a character array from a string, with a method like ToArray() or something. You can also fetch a CharacterIterator.Or even use a Regex.   