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

x
?
Solved

c++ - variable define types

Posted on 2014-03-14
7
Medium Priority
?
244 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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 668 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 1332 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 45

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 1332 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

824 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