Solved

Using char pointers in C/C++

Posted on 2010-09-07
15
766 Views
Last Modified: 2013-12-27
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
Comment
Question by:atomicgs12
  • 4
  • 3
  • 3
  • +3
15 Comments
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 33623507
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
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 166 total points
ID: 33623534
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
 
LVL 32

Expert Comment

by:phoffric
ID: 33623552
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
 
LVL 8

Assisted Solution

by:Subrat (C++ windows/Linux)
Subrat (C++ windows/Linux) earned 166 total points
ID: 33623720
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
 
LVL 32

Expert Comment

by:phoffric
ID: 33623767
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 33624180
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
 
LVL 40

Accepted Solution

by:
evilrix earned 168 total points
ID: 33624397
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 33624452
>>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
 
LVL 40

Expert Comment

by:evilrix
ID: 33624803
>> 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
 
LVL 3

Expert Comment

by:WiB
ID: 33626551
evilrix, why not

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

Expert Comment

by:WiB
ID: 33626578
Subrat2009, agree with you to the point that assigning default values to global variables let us to not introduce logical errors
0
 
LVL 40

Expert Comment

by:evilrix
ID: 33628407
>> evilrix, why not
Isn't that exactly what I said? What have I missed about your comment?
0
 
LVL 32

Expert Comment

by:phoffric
ID: 33631533
>> 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
 

Author Comment

by:atomicgs12
ID: 33816479
No objections
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Preface: This article is part of a series focused on cross platform mobile app development (specifically Android and iOS) using the Alloy framework and Titanium Studio made by Appcelerator (https://www.appcelerator.com/). This article presumes a wor…
As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now