Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

c++ - variable define types

Posted on 2014-03-14
7
Medium Priority
?
242 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 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 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 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

704 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