Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 811
  • Last Modified:

Using char pointers in C/C++

I have seen some c++ where there is code as follows:
char *myText;

if(1)
     myText = "This is the first part";
else
     my Text = "This is the second part";

printf(myText);

My concern is first is this correct? What happens to the pointer myText when the code goes out of scope? Shouldn't myText be initialized somehow first? I've never seen this done this way and also Visual Studio will spit out warnings.

Any suggestions as the proper way to code the above?

Thanks
0
atomicgs12
Asked:
atomicgs12
  • 4
  • 3
  • 3
  • +3
3 Solutions
 
Subrat (C++ windows/Linux)Software EngineerCommented:
No this is not a good idea. You first may reserve space statically(array)/dynamically using malloc()/calloc(). then use strncpy_s() to copy the string.

While declaring pointer, You may say char* myText = null;

0
 
phoffricCommented:
First, the obvious part: In the else body, you have my Text  instead of myText. But, I'm assuming that this is just a typo.

>> Visual Studio will spit out warnings.
What warnings do you get. I didn't get warnings at level W3; but at W4, I got only one warning (which I expected) applied to the if(1):
      warning C4127: conditional expression is constant
Naturally, if your test condition is a constant that evaluates to true, then the else condition is never going to be reached.

>> What happens to the pointer myText when the code goes out of scope?
If myText is defined in a function (or a block of code delineated by {}), then as with all auto variables, it is on the function's stack. When the function returns (or the } is reached), then all auto variables on the stack, including myText, become out of scope and are no longer valid symbols. You should think of myText as no longer existing.

>> myText = "This is the first part";
myText is a pointer to a char. "This is the first part" is a literal string constant.
myText now contains the address of the the first char, 'T' of this literal string constant. The literal string constant is allocated to a section of memory.

>> Shouldn't myText be initialized somehow first?
It is good practice to initialize all auto variables. If the usage is just as you show and nothing more, then it is not necessary to initialize myText. You are setting it to a value before using it, so that is fine.

>> Any suggestions as the proper way to code the above?
Naturally, the if(1) needs to be adjusted to something more meaningful. If I take your code literally, then the if statement can be removed, and replaced with the following initialization of myText:
   char *myText = "This is the first part";
0
 
phoffricCommented:
I just saw the first post.
>> You first may reserve space statically(array)/dynamically using malloc()/calloc().
    For the code in the OP, you could dynamically allocate memory and then copy the string.
      -- don't forget to deallocate the memory using free before the pointer goes out of scope.
     -- For this code in the OP, there is no need to dynamically allocate memory.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Subrat (C++ windows/Linux)Software EngineerCommented:
Another point,

Here string "This is the first part" is a constant string, So declaration should be const char*, but problem is we can't do it here.

Try:  char* p ="hello";
        *p = 'L'; //   No compile time error, but will get runtime error(undefined behaviour/ program Crash)

Use
   const char* p ="hello";
  *p = 'L";    // compile time error


0
 
phoffricCommented:
In your OP, you are showing myText that is being set to a literal string constant (a c-style string array), but which you are not trying to modify the array. If, on the other hand, you wish to modify the array, then you could do the following:
void foo() {
   char myText1[] = "This is the first part";
   char myText2[] = "This is the second part";
   myText1[12] = 'd';
   myText2[12] = 'f';
   printf("%s\n%s\n",myText1, myText2);
}

Open in new window

0
 
Jaime OlivaresCommented:
I have some disagreements with first post:

>No this is not a good idea. You first may reserve space statically(array)/dynamically using malloc()/calloc(). then use strncpy_s() to copy the string.
This is not needed in this case. Assignment to literals is perfectly valid. Of course, you shall not try to overwrite the pointed buffer.

>While declaring pointer, You may say char* myText = null;
In your specific example, there is no point in assigning myText to null. It will be assigned in next block anyways. Either the 'if' or the 'else' block.
Other cases where assignment is uncertain, then it would be a valid suggestion.

Jaime.
0
 
evilrixSenior Software Engineer (Avast)Commented:
Just elaborating on some things already said...In the context of C++ the example is making use of a deprecated feature. Literal strings are constant and assigning a non-const pointer to a constant literal is deprecated in C++ (but still fine in C). As from the next version of C++ (any due time soon) this code may not build.Other than that, the code you show is perfectly valid. A pointer just points to something and there is no reason it cannot point to a literal string. That string will live for the lifetime of the program. You cannot (as implied by the first line in this post) modify what the string points to but there is no reason it cannot point to it. As long as you have no plans to modify it and as long as to declare the pointer as a const char * rather than a char * the code is perfectly valid.The following is a good reference to learn more about pointers.http://www.cplusplus.com/doc/tutorial/pointers/>> Any suggestions as the proper way to code the above?See below
char const *myText = 0;

if(boolSomePredicate)
     myText = "This is the first part";
else
     myText = "This is the second part";

printf(myText);

// or a better way is like this...

char const * myText = boolSomePredicate ? "This is the first part" : "This is the second part";
printf(myText);

Open in new window

0
 
Subrat (C++ windows/Linux)Software EngineerCommented:
>>In your specific example, there is no point in assigning myText to null.  It will be assigned in next block anyways. Either the 'if' or the  'else' block.
Other cases where assignment is uncertain, then it would be a valid suggestion.


It's always better practise to assign degault value to variables while declaring.. And also it's safer program.

Andalso +1 to evilrix.....
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> It's always better practise to assign degault value to variables while declaring.. And also it's safer program.
Actually there are as many valid arguments for as there are against this idea. Let me elaborate of this rational...

Initialisation costs (even for intrinsic types) and if you are going to immediately set a value it's a waste of time. One might argue this time is negligible but it soon fails to be if the code is called repeatedly and is more pervasive if the type is a user defined type. The better solution is to always, where possible, make sure you can initialise a variable to a value you are going to use. A ternary operator (as I show above) can be one tool to manage this. In fact, about the only time this isn't really possible is when a variable is being used purely as an out parameter for a function (where it is passed in by reference and set in the function.

>> Andalso +1 to evilrix.....

Thank you :)
0
 
WiBCommented:
evilrix, why not

char const *myText = boolSomePredicate ? "This is the first part" : "This is the second part";
printf(myText);
0
 
WiBCommented:
Subrat2009, agree with you to the point that assigning default values to global variables let us to not introduce logical errors
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> evilrix, why not
Isn't that exactly what I said? What have I missed about your comment?
0
 
phoffricCommented:
>> It's always better practice to assign default value to variables while declaring.. And also it's safer program.

    I have been forced to follow this practice due to project standards that were strictly enforced by external bureaucrats (i.e., outside of the company).

    BUT, one disadvantage in a somewhat complex function is that if a path exists where the auto variable is not set before being used; then a forced initialization hides this error. If not forced, then either the debugger or extra run-time tools were used to identify set/use abuse. Now, if we are talking about a pointer being set to NULL, then you get an easy to diagnose crash; so no problem there. But, if you are initializing a numeric variable that will be set to some value not known at initialization time, then initializing it during definition is not an extra unnecessary step, but if the path taken uses the variable accidentally (i.e., a bug) before it wa set, then you may have a subtle problem to debug.
0
 
atomicgs12Author Commented:
No objections
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 4
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now