473,326 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Use of Pointer of Pointer

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
3 2369
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
16
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
8
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.