Main Topics
Browse All TopicsI have been playing around with learning C++ and noticed the following...
for instance:
char *someVariable;
what does the "*" do for "someVariable" in that position?
What does it mean?
Full points to an expert who can explain it in C++ newb terms and give additional examples (1 or 2) so I can understand it better.
Also, I do know Java so an example or explanation that relates to that may help as well.
Thanks,
tb55
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>> char *someVariable;
The * means that someVariable is a pointer to a char. Some more examples :
int *someIntPtr;
MyClass *someClassPtr;
means that someIntPtr is a pointer to an int, and someClassPtr is a pointer to a MyClass object.
Or, in other words, someVariable will contain the memory address of the char it is pointing to.
The above tutorial will explain that a lot better than I could write here, but just to add one thing - there's a subtle difference with the '*' operator, since it is used to dereference pointers also:
char buf[] = "test";
char* someVariable = &test[0];// let that poitn to the 1st letter of 'buf'
char letter; // create one char
letter = *someVariable; // here, '*' is used to assign the content of whre 'someVariable' points to, i.e 'dereferencing' the pointer
>>Also, I do know Java so an example or explanation that relates to that may help as well.
In Java, everything is a pointer.
Where as in C++, we can address an object by pointer, reference, or (value / concrete instance).
foo myfoo; //Concrete object
foo* mypointer = &myfoo; //pointer
foo& myref = myfoo; //Reference
In general, it's better to avoid using pointers in your code. For example, you can use std::string or std::vector<char> as replacements for char*.
>>>> what does the "*" do for "someVariable" in that position?
The position of the asterisk * may be somewhere between the type specifier and the variablename. C++ programmers mostly see the * as part of the type (making it a pointer type) rather than just have a different view on the variable (viewing it indirectly by its address). C programmers often put the asterisk to the variable, so as with your sample code.
The advantage of a pointer variable is that you can use the same variable pointing to different objects:
string str1 = "abc";
string str2 = "xyz";
string * pstr = &str1; // initially points to str1
pstr = &str2; // now points to str2
You even can set a pointer variable to a 'nil' value:
string * pstr = NULL;
The NULL at the right side is a macro that is equivalent to a integer 0 (that has historical reasons). So, different to a object you can check a pointer if it is valid by checking if it is NULL:
if (pstr == NULL)
pstr = new string("Hello World");
Here we create a dynamic object (at the heap) by calling 'new' operator. The 'new' operator always returns a pointer to the object created. By
string str = "any text";
we create a object at the stack. The object is valid as long as the current scope which is defined by a sequence of statements within curly brackets { } was not left:
if (any_condition)
{
string str = "any text";
...
}
// coming here the variable str isn't valid.
If you created a object by 'new' it keeps valid beyond the scope boundaries. It must be 'deleted' explicitly to free the storage (and run the destructor function if it is a class object).
string* pstr = NULL;
if (any_condition)
{
pstr = new string("any text");
...
}
// pstr is still valid if any_condition was true
if (pstr != NULL)
{
dosomething(pstr);
delete pstr; // delete the pointer after use
}
Note, if you don't delete pointers after use, you'll have a so-called memory leak that is a storage in memory that never was freed and therefore can't be used for other purposes.
If you have a pointer to a class object you will use the arrow operator -> to access data members or function members of the class:
class X
{
public:
int i;
void anyFunc();
};
X* px = new X; // creates a X using the default constructor
px->i = 5; // assign to data member
px->anyFunc(); // call member function
delete px;
For stack object the same sequence is
{
X x;
x.i = 5;
x.anyFunc();
}
// here x was destroyed
There are three main reasons for using pointers over objects:
1. When passed to a function, only an address needs to
be passed and not a (maybe big) data structure.
2. When having a class tree with baseclasses and derived classes,
you can access objects of different derived classes via
baseclass pointer.
class Base
{
virtual void do() = 0;
};
class Derived1 : public Base
{
virtual void do() { dosomething(); }
};
class Derived2 : public Base
{
virtual void do() { dosomthingelse(); }
};
Derived1 d1;
Derived2 d2;
Base* pBase = NULL);
if (any_condition)
pBase = &d1;
else
pBase = &d2;
// now call do member function virtually !!!
pBase->do();
The latter calls the overloaded do function of either derived class.
3. When using a array in C/C++ it was treated like a pointer to the first element:
void func (int* parr, int siz)
{
for (int i = 0; i < siz; ++i)
parr[i] = i;
}
int arr[10]; // define a array of 10 integers
func(arr, 10); // passing a array
int* parr = new int[10]; // create 10 integers at the heap
func(parr, 10); // passing a pointer (dynamic array)
You see, the array variable 'arr' turned to a pointer variable when passed to a function. Note, the 'p' I added to any pointer variable is convention only. In the called function you can't find out the size of the array. That's why I passed an additional size argument (what is highly recommended but optional).
As Axter explained you should avoid pointers in C++ if possible. Case 1 can be easily solved by using a reference type when passing objects to functions. A reference is an address same as a pointer but it is a valid address and cannot be NULL or invalid. Case 3 can be solved by using a container object like std::string or std::vector which allows to define dynamic 'arrays' and pass them by value or by reference. For case 2 you can use a baseclass reference when passing a derived object to a function. You only need to use baseclass pointers when storing the pointers (addresses) to a container. But that wasn't a problem for a newbee.
Regards, Alex
Business Accounts
Answer for Membership
by: jkrPosted on 2007-06-20 at 06:22:08ID: 19324293
>>what does the "*" do for "someVariable" in that position?
It declares 'someVariable' as a pointer to 'char', as opposite to a 'char' variable (holding one character)