Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 17th, 2009, 08:43 AM
Registered User
 
Join Date: Oct 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Strings (differences between an array of char and *char)

Hello,

Could please someone explain me why these two senteces don't cause a execution error

char letters[]="ABCDE";
letters[0]='X';

whereas these two sentences do cause and error in execution time?

char* letters2="ABCDE";
*(letters2)='X';

I understand that what I am doing in the first block is to create an array called letters with the values A,B,C,D,E and /n.

In the second block, from what I have understood from my manual, you are creating a pointer to char, and at the same time, you are initialiazing the value for that memory position with the letter A, and the following positions with B,C,D,E, and /n.

But what I don't understand is why I cannot access the position pointed by letters2, and change the first letter.

Thank you in advance for your help.
Reply With Quote
  #2 (permalink)  
Old August 14th, 2010, 03:12 AM
Authorized User
 
Join Date: Nov 2009
Posts: 26
Thanks: 1
Thanked 1 Time in 1 Post
Post

After assigning a string value to a variable of type char*, you use index to access it's elements. Just like array of chars.
Code:
char* letters2="ABCDE";
letters2[0]='X';
Although letter2 itself is a pointer to the first element of the string, you can't change it's value using asterisk(*) operator.
you can test this simply by printing memory addresses in a console application..

Hope that helped...
Reply With Quote
  #3 (permalink)  
Old August 30th, 2010, 08:02 PM
Registered User
 
Join Date: Jun 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Lightbulb

yes, an example here c++ pointer address when using a pointer to point to a memory address in a simplified example without even using an array.
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Big challenge here! How to convert char* to char^? samiswt Visual C++ 2005 1 November 30th, 2007 09:09 PM
Invalid conversion from 'char*' to 'char' deuxmio C++ Programming 3 December 8th, 2006 07:56 AM
Basic char array and cin query. gillianbc C++ Programming 4 November 4th, 2004 07:44 PM
Char array cin query (additional) gillianbc C++ Programming 3 October 29th, 2004 03:22 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.