C function argument char * vs char [] -


I think this question is of an extension. Say I have the following code:

  #include & lt; Stdio.h & gt; #include & lt; String.h & gt; Zero func (char * str) {strcpy (str, "test"); } Int main () {char testStr [20] = "original"; Function (testStr); Printf ("% s \ n", testStr); / * After a print "test" a new line * / return 0; }   

From my understanding, do not expect func to be an indicator as an argument for a read-only literally needed? Whereas, what is being taught is copy on literal word piles only for reading

Although this yield is the right result, is it doing 100% right? Will the readability of the code improve if func () are accepted char [] instead of char * ?

Your understanding is right that the code is okay.

The following, on the other hand, are not:

  void func (char * str) {strcpy (str, "test"); } Int main () {char * testStr = "original"; Function (testStr); }   

This attempts to modify a string, which results in undefined behavior.

According to the question of readability, that is subjective.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -