By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,878 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

Use of Pointer of Pointer

P: n/a
raj
Hi all.
I am little bit confsused with the way pointer is working. First of all
what is the difference between
int &Number = 10;
and
int *Number;

One more question ...

void Copy(char **word)
{
*word = [some pointer to a string];
}

when calling this function with
char **sentence;
Copy(sentence); //Here the application crashes

but when calling this function with
char *sentence;
Copy(&sentence); //Here it works fine.

What is wrong i do in the first call of function.
Thanks in advance
Rajat

Nov 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
int &Number = 10;
shouldn't work since you are trying to assign a literal to a reference
variable.

int *Number;
declares a pointer to an integer

They are different in that references must be initialized with a valid
value and can not point to anything else in its lifetime. It can not
point to NULL. Also, references don't need to be dereferenced like you
do with pointers.

Regarding your copy question:
I don't think there's anything wrong with calling Copy with a char**
versus &(char*). Perhaps it isn't being initialized properly. I don't
know the C++ way but in C you can do something like this:

char** sentence = (char**)malloc(sizeof(char)*LEN*NUMSTR);
sentence[0] = "blah";

Nov 22 '05 #2

P: n/a
raj wrote:
Hi all.
I am little bit confsused with the way pointer is working. First of all
what is the difference between
int &Number = 10;
and
int *Number;
int & Number = 10;
This is an error. Only const references can be initialized with
temporaries.
You can do this :
int temp = 10;
int & Number = temp;

Now Number is a reference to an integer, initialized with temp.

Alternatively, you can also use :
const int & Number = 10;

constant references can be initialized by literals and temporaries.

Further, int *Number defines an un-initialized pointer to integer. The
pointer is named as "Number".
One more question ...

void Copy(char **word)
{
*word = [some pointer to a string];
}

when calling this function with
char **sentence;
Copy(sentence); //Here the application crashes

but when calling this function with
char *sentence;
Copy(&sentence); //Here it works fine.

What is wrong i do in the first call of function.


Saying "char** sentence" defines only a pointer to char*. It doesnot
reserve any memory for the char*. Hence, when you dereference char **
inside the function, you get some junk value and that memory doesnot
belong to you. Attempting to write on that memory yields a seg-fault.

In the second case, when you say char *sentence, you are reserving the
space for char* on the stack. So that memory is yours, and the copy
function is legally writing in your stack area. Hence no crash.

Nov 22 '05 #3

P: n/a
raj wrote:
Hi all.
I am little bit confsused with the way pointer is working. First of all
what is the difference between
int &Number = 10;
this won't compile.

int a=10;
int& Number = a;
is ok.
Number is an alias (different name) for 'a' here.

This makes more sense for more complex expressions like:

int a[4]= {1,2,3,4};
int& Number = a[2];

or as arguments to function calls.

and int *Number;
this is a (non-initialized) pointer to an int.

One more question ...

void Copy(char **word)
{
*word = [some pointer to a string];
}

when calling this function with
char **sentence;
Copy(sentence); //Here the application crashes
You dereference a non-initialized pointer.

sentence points to some char*

But as it is not initialized...
Assume sentence is (by accident) address 0x0123

Using *sentence
your machine assumes at 0x0123 a char* and thus
writes [some pointer to a string] to address 0x0123.
But 0x0123 might by not accessible for writing, hence the crash.

char* sentencePtr;
char** sentence = &sentencePtr;

would work.

Now the memory location of sentencePtr is accessed
when using *sentence.

but when calling this function with
char *sentence;
Copy(&sentence); //Here it works fine.


you take the address of a pointer
then you derefernece it.

*(&sentence) is sentence
so you assign [some pointer to a string] to sentence, which is ok.

Cheers,
Marc

Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.