character pointers

THE_RB

Joined Feb 11, 2008
5,438
I don't think so and what are the safety benefits you are talking about?
For a beginner that is struggling with referencing contiguous data the use of pointer referencing; p+i+j has potential to cause problems.

Referencing the same contiguous data in this fashion; p[i+j] removes ambiguity.
 

debjit625

Joined Apr 17, 2010
790
For a beginner that is struggling with referencing contiguous data the use of pointer referencing; p+i+j has potential to cause problems.

Referencing the same contiguous data in this fashion; p[i+j] removes ambiguity.
That depends on OP,and as far my post is concern the problem of the OP has been solved.
 

debjit625

Joined Apr 17, 2010
790
That's such an ugly use of pointers. This type of referencing below has a LOT of safety benefits for simple string tasks
Rich (BB code):
#include <stdio.h>
#include <iostream>
int main ()
{ 
  char* p = "hello";     // compiler inserts NULL
  char m[10];            // is long enough to hold any data you need
  int i = 0;
  int j = 0;
  while(p != 0)       // break on input string NULL found
  {
      if (p != 'l')   // if input not 'l'
      {
          m[j] = p;   // copy to output
          j++;
      }
      i++;
  }
  m[j] = 0;              // must insert NULL at output string end
  puts(m);
  system("pause");
  return 0;
}


I have already corrected this to the OP, but again you made the same mistake. In a C program we don't use <iostream>, as it is from C++ Standard library. Most compiler will compile this as they can both compile code for C and C++.
But if you still do this it will result a C program which is dependent on C++ Standard Library i.e.. not a C program.

Another one
Rich (BB code):
int main ()
{
return 0;
}
In C++ this function means that it will not take any arguments, but in C it means this function will take endless arguments i.e..an indeterminate number of arguments and C disables all type checking in that case which cause bugs errors in your code hard to eliminate. So the way to do it in C is.
Rich (BB code):
int main (void)
{
return 0;
}
Both in C and C++ this function means it will not accept any arguments as the void keyword means “nothing” in this case.
 

THE_RB

Joined Feb 11, 2008
5,438
I didn't write that code. I specifically addressed the pointer code, doing so as an edit within the OP's original code. :)
 
Top