Solved

c++ - variable define types

Posted on 2014-03-14
7
239 Views
Last Modified: 2014-04-04
Hi,

What is difference in defining string like 2 below methods:

1)  std::string str = "Test string";

2)   char* cstr = "Test string";

What is difference and which one is better approach.

Thanks
0
Comment
Question by:josephm67
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 167 total points
ID: 39929391
You could say the difference is like the difference between chalk and cheese.
They are two rather different things.

std::string is a class with various functions built in to handle strings.

char* is a pointer to a memory location, the memory is the start of a narrow character 'array'

Better - depends on what you want to do with them.  For some things the first is better, for others the second is a better approach.
0
 
LVL 29

Assisted Solution

by:pepr
pepr earned 333 total points
ID: 39929464
To add, the "string literal" is always the sequence enclosed in double quotes, in contiguous memory, and (invisibly) ended with the character with ordinal value zero. The mentioned pointer is the address to the first character. It is the older approach that comes from the C language. Having to work with memory blocks like that is often error prone.

The first case also uses the string literal, but its content is only used to initialize the str object. You should learn basics of both approaches, but you probably will tend to use the std::string later.
0
 
LVL 86

Expert Comment

by:jkr
ID: 39930596
Another significant difference s that

char* cstr = "Test string";

Open in new window


is strctly 'const' - any attempt to alter it wll cause your application to crash because of an access violation.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:josephm67
ID: 39931196
thanks for all.

Can anyone tell me what is advantage of using pointers? I have c# experience. I do not see any advantage of using pointers because we have to set a length(I guess) and also if we change pointer value then it can crash application. So, it is difficult to handle. Why to use pointer if we have option to use string or other data type. What are ideal cases to use pointers?

Thanks
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39931221
Speed is one advantage.
This usage you have results in a constant value you can't change.  Usage in another way gives variables you can change the value of.

(Some windows/third party API functions also require pointers - so you must use a pointer in that case)
0
 
LVL 29

Accepted Solution

by:
pepr earned 333 total points
ID: 39931893
The more points of view, the bigger is the chance for "Aha!". :) Whenever you need to store a value in a memory, you need a space big enough and a location of that space -- independently on whether you need to store constant, variable content, accessible directly, via a reference via a pointer.

The location is expressed by the address of the first element,  the size must also be known.

Now the things starts to be interesting, because it depends on how the programming language works with the memory and the variables.

1. Or the block of memory has well known position and well known size. Any code that needs the value knows the address where to find it. This is the case when the block of memory was placed at compile time. This holds also for blocks allocated at stack.

2. Or the code needs to learn where the block is located. It needs to get the address. the address is 8 bytes in 64-bit OS. And the address can be called a pointer value -- it points to the memory block. And you usually need another memory block of the size 8 bytes to store the address -- suddenly you have a pointer variable.

3. Say, in C++, you have a p pointer variable which stores 8 bytes at the well known address (because the compiler said so). At first step,you get the content of the pointer variable (i.e. another address). At second step, you get the access to you wanted target data.

4. In C#,  it is similar. The only difference is that the extra step is done automatically (automatic dereferencing). This way it looks you work directly with the target object, but it is only illusion.

5. When passing a variable in C#, you almost always pass the 8 bytes (the reference). On the contrary, you can use three ways to pass the arguments in C++.

Pointers are actually an older form of references. They do not use automatic dereferencing, and they. have some extra capabilities (like pointer arithmetics).
0
 

Author Closing Comment

by:josephm67
ID: 39978257
thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

734 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