Solved

c++ - variable define types

Posted on 2014-03-14
7
232 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
Comment Utility
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 28

Assisted Solution

by:pepr
pepr earned 333 total points
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:josephm67
Comment Utility
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
Comment Utility
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 28

Accepted Solution

by:
pepr earned 333 total points
Comment Utility
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
Comment Utility
thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now