dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: pointers and strings

  1. #1
    Join Date
    Aug 2006
    Location
    Dallas, TX
    Posts
    47

    Question pointers and strings

    Hey guys I know this is a very basic question. But i'm following along with a book. Granted the book is not for the most recent version of c++. But i'm unable to find any information on the problem i'm having.

    In the book example it's using a declaration of:

    Code:
    char* pstr1 = "A string here";
    I'm using visual studio 2019. And i'm getting an error that "a value type of const char* cannot be used to initialize an entity type of char*". Is the book wrong? Or is there a change that's happened in c++ since 2015 that makes this now invalid.

    Full code here:
    Code:
    // Ex4_06.cpp
    // Initializing pointers with strings
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    
    
    int main()
    {
    	char* pstr1 = "Robert Redford";
    	char* pstr2 = "Hopalong Cassidy";
    	char* pstr3 = "Lassie";
    	char* pstr4 = "Slim Pickens";
    	char* pstr5 = "Boris Karloff";
    	char* pstr6 = "Oliver Hardy";
    	char* pstr = "Your lucky star is ";
    
    
    	int dice = 0;
    
    
    	cout << endl
    	 	 << " Pick a lucky star!"
    		 << " Enter a number between 1 and 6: ";
    	cin >> dice;
    
    
    	cout << endl;
    	switch (dice)
    	{
    	case 1: cout << pstr << pstr1;
    		break;
    	case 2: cout << pstr << pstr2;
    		break;
    	case 3: cout << pstr << pstr3;
    		break;
    	case 4: cout << pstr << pstr4;
    		break;
    	case 5: cout << pstr << pstr5;
    		break;
    	case 6: cout << pstr << pstr6;
    		break;
    
    
    	default: cout << "Sorry, you haven't got a lucky star.";
    	}
    
    
    	cout << endl;
    	return 0;
    }
    Last edited by cypher5783; January 12th, 2020 at 03:00 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,579

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,971

    Re: pointers and strings

    The book wasn't absolutely correct. That form of initialisation was always not correct - but tolerated by the then standard/compilers. Later standards of C++ correctly do not allow this. It needs to be at least:

    Code:
    const char* pstr1 = "A string here";
    and preferably:

    Code:
    const char* const pstr1 = "A string here";

    So that neither the contents of the memory, nor the pointer pstr1 itself can be changed. The contents shouldn't be changed as the memory allocated by the compiler in this form of initialisation could be part of the program itself rather than any specific run-time allocation. Also, if pstr1 is changed then you don't know the start of the string.

    If you want to change the contents, then define as:

    Code:
    char pstr1[] = "A string here";
    Also note that this is very 'C' style code and that in C++ the string class should be used. You are not being taught good C++ practice. IMO, ditch the book and get one that covers C++17 and modern C++ coding style.
    Last edited by 2kaud; January 12th, 2020 at 06:00 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.2)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)