Solved

c++ - variable define types

Posted on 2014-03-14
7
237 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 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
Find Visual Studio Tools 2 113
source code to scan multiple images 6 95
Header Errors LNK2019, LNK1120 - Unresolved Externals 4 194
No module found pypyodbc, 3 33
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 clear a vector as well as how to detect empty vectors in C++.

831 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